AcWing 837. 连通块中点的数量 C++
原题链接
简单
作者:
自由周某
,
2020-08-10 15:55:39
,
所有人可见
,
阅读 429
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e5 + 5;
int a[N],fa[N],num[N];
int find(int x){
if(fa[x] == x) return fa[x];
else
fa[x] = find(fa[x]);
return fa[x];
}
int main(){
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = 1; i <= n; i++) num[i] = 1;
while(m--){
string s;
cin>>s;
if(s == "C") {
int a, b;
cin >> a >> b;
if(a == b || find(a) == find(b)) continue;
else{
num[find(b)] += num[find(a)];
fa[find(a)] = find(b);
}
}
else if(s == "Q1"){
int a, b;
cin >> a >> b;
if(find(a) == find(b)) puts("Yes");
else puts("No");
}
else {
int a;
cin>>a;
cout << num[find(a)] << endl;
}
}
return 0;
}