tamamocross @ 2022-12-08 15:35:08
#include<iostream>
int N,M;//N是元素个数,M是操作个数
using namespace std;
struct Disjoint_set{
int father[10001];
void ini(){
for(int i=1;i<=N;i++){
father[i]=i;//父亲初始化为本身
}
}
int FindAncestor(int i){//找祖先
if(i==father[i]){
return father[i];
}
father[i]=FindAncestor(father[i]);
return father[i];
}
}set;
int main(){
cin>>N>>M;
set.ini();
int opo,x1,x2;
for(int i=1;i<=M;i++){
cin>>opo>>x1>>x2;
if(opo==1){
set.father[x1]=set.FindAncestor(x2);
}
if(opo==2){
int tmp1=set.FindAncestor(x1),tmp2=set.FindAncestor(x2);
if(tmp1==tmp2){
cout<<'Y'<<endl;
}else{
cout<<'N'<<endl;
}
}
}
}
by Dream_weavers @ 2022-12-08 15:52:36
if(opo==1){
set.father[x1]=set.FindAncestor(x2);
}
改成
if(opo==1){
set.father[set.FindAncestor(x1)]=set.FindAncestor(x2);
}
by Dream_weavers @ 2022-12-08 15:53:19
@tamamocross
by tamamocross @ 2022-12-09 09:49:19
@Dream_weavers 过了,感谢