并查集
2025-05-05 19:05:51
发布于:北京
T1:
#include<bits/stdc++.h>
using namespace std;
int n,m,fa[10010];
int get(int x){
if(fa[x]==x) return x;
return fa[x]=get(fa[x]);
}
void merge(int x,int y){
fa[get(x)]=get(y);
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)fa[i]=i;
while(m--){
int x,y,z;
cin>>z>>x>>y;
if(z==1){
if(get(x)!=get(y)){
merge(get(x),get(y));
}
}
else{
if(get(x)==get(y))cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}
return 0;
}
这里空空如也
有帮助,赞一个