code
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int fa[N], a[N];
int cnt[N];
int find(int x){
if(x != fa[x]) fa[x] = find(fa[x]);
return fa[x];
}
void un(int x, int y){
x = find(x);
y = find(y);
if(x != y){
fa[y] = x;
cnt[x] += cnt[y];
}
}
int main(){
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
fa[i] = i;
cnt[i] = 1;
}
string s;
int a, b;
while(m--){
cin >> s;
if(s == "C"){
cin >> a >> b;
if(a != b) un(a,b);
}else if(s == "Q1"){
cin >> a >> b;
if(a == b) cout <<"Yes"<<endl;
else{
a = find(a);
b = find(b);
if(a == b) cout <<"Yes"<<endl;
else cout <<"No" <<endl;
}
}else{
cin >> a;
a = find(a);
cout << cnt[a]<<endl;
}
}
return 0;
}
标签:连通,837,int,cnt,cin,fa,中点,find,cout
From: https://www.cnblogs.com/index-12/p/17297205.html