题解
2026-08-17 10:01:11
发布于:浙江
0阅读
0回复
0点赞
#include <iostream>
using namespace std;
int fa[200005];
int n,m;
int find(int x){
if(fa[x] == x)return x;
return fa[x] = find(fa[x]);
}
void join(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx != fy)fa[fx] = fy;
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i++)fa[i] = i;
while(m--){
int a,b,c;
cin >> a >> b >> c;
if(a == 1){
join(b,c);
}else{
if(find(b) == find(c)){
cout << "Y" << endl;
}else{
cout << "N" << endl;
}
}
}
}
这里空空如也






有帮助,赞一个