#include <iostream>
using namespace std;
const int maxn=1e4+4;
int f[maxn];
int find(int x)
{
while(x!=f[x])
x=f[x]=f[f[x]]; //循环版本的路径压缩
return x;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
f[i]=i;
}
for(int i=1;i<=m;i++)
{
int z,x,y;
cin>>z>>x>>y;
int a=find(x);
int b=find(y);
if(z==1) //合并
{
f[a]=b;
}
else //查询
{
if(a==b)
cout<<"Y"<<endl;
else
cout<<"N"<<endl;
}
}
return 0;
}
查并集 模板
猜你喜欢
转载自blog.csdn.net/xiaolan7777777/article/details/109063838
今日推荐
周排行