肖泽源 @ 2018-08-17 15:34:56
#include<bits/stdc++.h>
using namespace std;
int f[200000+10];
int find(int x)
{
if(x!=f[x])return find(f[x]);
return f[x];
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)f[i]=i;
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&z,&x,&y);
if(z==1)
{
int r1=find(x),r2=find(y);
if(r1!=r2)f[r1]=r2;
}
else if(z==2)
{
int r1=find(x),r2=find(y);
if(r1==r2)printf("Y\n");
else printf("N\n");
}
}
return 0;
}
by forlight @ 2018-08-17 15:41:27
你没有路径压缩,find函数应该这样写
int find(int x){
if(f[x]!=x)f[x]=find(s[x]);
return f[x];
}
by forlight @ 2018-08-17 15:42:14
@forlight
int find(int x){
if(f[x]!=x)f[x]=find(f[x]);
return f[x];
}
by 肖泽源 @ 2018-08-17 15:47:15
@forlight 已经AC了,谢谢
by Carbon @ 2018-09-18 23:22:56
@forlight 感谢 , 好久没写并查集都忘掉了,谢谢提醒