simple1first @ 2023-12-18 21:09:09
#include<bits/stdc++.h>
using namespace std;
const int v = 1e5+3;
int fa[v],n,m,x,y,z;
int root(int x){
return fa[x]==x ? x : fa[x] = root(fa[x]);
}
int merge(int x,int y){
fa[root(x)] = root(y);
}
bool con(int x,int y){
return root(x)==root(y);
}
int init(int n){
for(int i=0;i<n;i++){
fa[i] = i;
}
}
int main(){
cin >> n >> m;
init(n);
while(m--){
cin >> z >> x >> y;
if(z==1)merge(x,y);
else if(z==2)printf("%c\n",con(x,y) ? 'Y' : 'N');
}
return 0;
}
by Luzhuoyuan @ 2023-12-18 21:24:33
@simple1first merge
和 init
的返回类型错了,以及 init
中没有给 fa[n]
赋初值。
by simple1first @ 2023-12-19 20:27:52
@Luzhuoyuan 谢谢