大意:
告诉除自己以外的相对位置,判断绝对位置是否有冲突
分析:
我们可以让相对位置连单向边,代表位置关系,如果有环就说明有冲突了,判环用拓扑排序即可
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using i128 = __int128;
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long
//找环有拓扑排序更快,spfa很容易TLE
int n,k;
vector<int> g[200010];
void solve(){
cin>>n>>k;
vector<int> in(n+1);
for(int i = 1;i<=n;++i) g[i].clear();
for(int i = 1;i<=k;++i){
vector<int> a(n+1);
for(int j = 1;j<=n;++j){
cin>>a[j];
if(j>=3){
g[a[j-1]].push_back(a[j]);
in[a[j]]++;
}
}
}
queue<int> q;
for(int i = 1;i<=n;++i){
if(!in[i]) q.push(i);
}
int tot = 0;
while(!q.empty()){
int x = q.front();
q.pop();
++tot;//顶点数
for(auto y:g[x]){
in[y]--;
if(!in[y]) q.push(y);
}
}
cout<<(tot==n?"YES":"NO")<<"\n";
}
signed main(){
ios;
int t;cin>>t;
while(t--){
solve();
}
return 0;
}
vp的时候写了个spfa判环T了,一开始还想写并查集判环的,但是忘记怎么写了