AcWing 837. 连通块中点的数量
原题链接
简单
作者:
Value
,
2020-05-11 22:52:23
,
所有人可见
,
阅读 550
#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int parent[N], Size[N];
void init(){
for(int i = 0; i < N; i ++ ){
parent[i] = i;
Size[i] = 1;
}
}
int find(int t){
if(parent[t] == t) return t;
return parent[t] = find(parent[t]);
}
int main(){
cin >> n >> m;
char op[5];
int a, b;
init();
while(m -- ){
cin >> op;
if(op[0] == 'C'){
cin >> a >> b;
if(find(a) == find(b)) continue;
Size[find(b)] += Size[find(a)];
parent[find(a)] = find(b);
}else if(op[1] == '1'){
cin >> a >> b;
if(find(a) == find(b)) cout << "Yes" << endl;
else cout << "No" << endl;
}else{
cin >> a;
cout << Size[find(a)] << endl;
}
}
return 0;
}