最近看ML看的晕,各种数学不会,特么的最优化就算了还泛函数,能虐死你啊,还是刷刷bc补充点能量比较好
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
#include<bitset>
using namespace std;
#define i64 long long
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("out.txt","r",stdin)
#define write freopen("out2.txt","w",stdout)
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
#define MID ((l+r) >> 1)
#define lc rt << 1
#define rc rt << 1 | 1
const i64 inf64 = 0x2f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int mod = 1000000007;
const int maxn = 101;
struct zz{
int x;
int y;
int turn;
const bool operator < (const zz & cmp) const{
if (x != cmp.x){
return x < cmp.x;
}
else{
return y < cmp.y;
}
}
const bool operator == (const zz & cmp) const{
if (x == cmp.x && y == cmp.y) {
return true;
}
else{
return false;
}
}
}zx;
int n, m;
int f[maxn][maxn];
bool dp[maxn][maxn][2];
vector<zz>out[maxn][maxn];
int cnt[maxn][maxn];
vector<zz>in[maxn][maxn];
int Hash[maxn][maxn];
void bfs(){
queue<zz>q;
for (int x = 1; x <= n; x++){
for (int y = 1; y <= n; y++){
if (x < y){
if (dp[x][y][0] == true){
zx.x = x;
zx.y = y;
zx.turn = 0;
q.push(zx);
}
if (cnt[x][y] == out[x][y].size()){
dp[x][y][1] = true;
zx.x = x;
zx.y = y;
zx.turn = 1;
q.push(zx);
}
}
}
}
zz now, to;
while (!q.empty()){
now = q.front();
q.pop();
if (now.turn == 0){
for (int i = 0; i < in[now.x][now.y].size(); i++){
to = in[now.x][now.y][i];
cnt[to.x][to.y]--;
if (cnt[to.x][to.y] == 0){
dp[to.x][to.y][1] = true;
to.turn = 1;
q.push(to);
}
}
}
else{
for (int i = 0; i < in[now.x][now.y].size(); i++){
to = in[now.x][now.y][i];
if (dp[to.x][to.y][0] == false){
dp[to.x][to.y][0] = true;
to.turn = 0;
q.push(to);
}
}
}
}
}
bool start(){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
in[i][j].clear();
out[i][j].clear();
cnt[i][j] = 0;
dp[i][j][0] = false;
dp[i][j][1] = false;
Hash[i][j] = 0;
}
}
for (int i = 1; i <= m; i++){
for (int x = 1; x <= n; x++){
for (int y = 1; y <= n; y++){
if (x<y)
if (f[i][x] == f[i][y]){
dp[x][y][0] = true;
dp[y][x][0] = true;
}
}
}
}
zz now, to;
int hashcnt = 0;
for (now.x = 1; now.x <= n; now.x++){
for (now.y = 1; now.y <= n; now.y++){
if (now.x < now.y){
hashcnt++;
for (int i = 1; i <= m; i++){
to.x = f[i][now.x];
to.y = f[i][now.y];
if (to.x > to.y){
swap(to.x, to.y);
}
if (to.x != to.y){
if (Hash[to.x][to.y] != hashcnt){
Hash[to.x][to.y] = hashcnt;
cnt[now.x][now.y]++;
in[to.x][to.y].push_back(now);
}
}
}
}
}
}
bfs();
for (int x = 1; x <= n; x++){
for (int y = 1; y <= n; y++){
if (x < y){
if (dp[x][y][0] == false){
return false;
}
}
}
}
return true;
}
int main(){
int T;
cin >> T;
while (T--){
cin >> n >> m;
for (int i = 1; i <= m; i++){
for (int j = 1; j <= n; j++){
//cin >> f[i][j];
SS(f[i][j]);
}
}
if (start()){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return 0;
}