6dypi @ 2017-02-11 10:05:54
#include<cstdio>
#include<cmath>
using namespace std;
int search(int x);
void mix(int a,int b);
int pre[10001];
int main()
{
int n,m;
int y,ss,tt;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
pre[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&y,&ss,&tt);
if(y==1) mix(ss,tt);
else if(search(ss)==search(tt)) printf("Y\n");
else printf("N\n");
}
return 0;
}
int search(int x)
{
int r=x;
while(pre[r]!=r)
{r=pre[r];}
return r;
}
void mix(int a,int b)
{
int fa=search(a),fb=search(b);
if(fa!=fb)
pre[fa]=fb;
}
by Lyrics @ 2017-07-23 15:08:57
void unionn(int i,int j){
int r1=find(i),r2=find(j);
if(r1==r2)return;
father[r2]=r1;//这句可以考虑不要
father[j]=r1;//这句一定要加上去不然没路径压缩会超时(我就是这一句忘了加导致TLE3个点)
return;
}
所以大佬应该是路径压缩没压缩好
void mix(int a,int b)
{
int fa=search(a),fb=search(b);
if(fa!=fb){
pre[fa]=fb;
pre[a]=fb;
}
return;
}