#include<bits/stdc++.h>
using namespace std;
int n,m;
int z,x,y;
int a[1000000];
int find(int k){
if(a[k] == k){
return k;
}else{
return a[k] = find(a[k]);
}
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i++){
a[i] = i;
}
for(int i = 1;i <= m;i++){
cin >> z >> x >> y;
if(z == 2){
if(find(x) == find(y)){
cout << "Y" << endl;
}else{
cout << "N" << endl;
}
}else{
a[find(y)] = find(x);
}
}
return 0;
}
并查集模板
最新推荐文章于 2024-09-10 08:28:20 发布