**如题,现在有一个并查集,你需要完成合并和查询操作。**
n个元素,m次操作
#include<bits/stdc++.h>
using namespace std;
int n,m,x,a,b;
int root[10001];
int anc(int x){
if(root[x]==0) return x;
else{
root[x]=anc(root[x]);
return root[x];
}
}
void link(int u,int v){
u=anc(u);
v=anc(v);
if(u!=v) root[u]=v;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>x>>a>>b;
if(x==1){
link(a,b);
}
else {
if(anc(a)==anc(b)) cout<<'Y';
else cout<<"N";
cout<<endl;
}
}
}
并查集
最新推荐文章于 2024-07-29 20:58:50 发布