裸题。
看之前的模版讲解吧,这里不再赘述了。
#include<cstdio>
#include<cstring>
using namespace std;
int fa[100010];
bool rel[100010];
int n,m,T;
int findroot(int x){
if(x==fa[x]){
return x;
}
int Fa=findroot(fa[x]);
if(rel[x]==0 && rel[fa[x]]==0){
rel[x]=0;
}
else if(rel[x]==0 && rel[fa[x]]==1){
rel[x]=1;
}
else if(rel[x]==1 && rel[fa[x]]==0){
rel[x]=1;
}
else if(rel[x]==1 && rel[fa[x]]==1){
rel[x]=0;
}
return fa[x]=Fa;
}
int main(){
// freopen("meciul.in","r",stdin);
// freopen("meciul.out","w",stdout);
int x,y;
scanf("%d",&T);
for(;T;--T){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
fa[i]=i;
}
memset(rel,0,sizeof(rel));
for(int i=1;i<=m;++i){
scanf("%d%d",&x,&y);
int f1=findroot(x),f2=findroot(y);
if(f1==f2){
if(rel[x]==rel[y]){
puts("NO");
}
else{
puts("YES");
}
}
else{
fa[f1]=f2;
if(rel[x]==0 && rel[y]==0){
rel[f1]=1;
}
else if(rel[x]==0 && rel[y]==1){
rel[f1]=0;
}
else if(rel[x]==1 && rel[y]==0){
rel[f1]=0;
}
else if(rel[x]==1 && rel[y]==1){
rel[f1]=1;
}
puts("YES");
}
}
}
return 0;
}