//并查集一定要压缩路径!
#include<iostream>
using namespace std;
int x[10005];
int get_father(int t){
if(t==x[t])
return t;
return x[t]=get_father(x[t]); //路径压缩!!!重点!!!
}
int main(){
int N,M,t0,t1,t2;
cin>>N>>M;
for(int i=0;i<N;i++) x[i]=i;
for(int i=0;i<M;i++){
cin>>t0>>t1>>t2;
if(t0==1) x[get_father(t1)]=get_father(t2);
if(t0==2) {
if(get_father(t1)==get_father(t2))cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}
return 0;
}
洛谷P3367并查集
最新推荐文章于 2024-07-22 09:43:39 发布