题目描述
通畅
C++ 代码
#include <iostream>
using namespace std;
const int N = 100010;
int p[N];
int cnt[N];
int find(int x){
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
int main(){
int n , m;
cin >> n >> m;
for(int i = 1; i <= n;++i){
p[i] = i;
cnt[i] = 1;
}
while(m--){
string op;
int a , b;
cin >> op;
if(op == "C"){
cin >> a >> b;
int x = find(a) , y = find(b);
if(x != y){
p[x] = p[y];
cnt[y] += cnt[x];
}
}else if(op == "Q1"){
cin >> a >> b;
cout << (find(a) == find(b) ? "Yes" : "No") << endl;
}else{
cin >> a;
cout << cnt[find(a)] << endl;
}
}
return 0;
}