社区讨论
50分求助
P3367【模板】并查集参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mhj22ra2
- 此快照首次捕获于
- 2025/11/03 19:27 4 个月前
- 此快照最后确认于
- 2025/11/03 19:27 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
int n,m,s[1000005];
int find(int x){
while(x!=s[x]){
x=s[x];
}
return x;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
s[i]=i;
}
while(m--){
int op,x,y;
cin>>op>>x>>y;
int a=find(x),b=find(y);
if(op==1){
s[a]=b;
}else if(op==2){
if(a==b){
cout<<"Y"<<endl;
}else{
cout<<"N"<<endl;
}
}
}
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...