#include <iostream>
#include <algorithm>
using namespace std;
const int N =1e5+7;
int p[N], c[N];
int n, m;
int find(int x){
if(x != p[x]) p[x] = find(p[x]);
return p[x]; //肯定不能是x啊 因为存在当前的根是其他节点
}
int main(){
string str;
cin>>n>>m;
for(int i = 0; i <= n; i++){
p[i] = i;
c[i] = 1;
}
int u, v;
while(m--){
cin>>str>>u;
if(str == "C"){
cin>>v;
if(u != v){
int a = find(u), b = find(v);
if(a != b) {
p[b] = a;
c[a] += c[b];
}
}
}else if(str == "Q1"){
cin>>v;
int a = find(u), b = find(v);
if(a != b) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}else{
cout<<c[find(u)]<<endl;
}
}
return 0;
}
837. 连通块中点的数量
最新推荐文章于 2022-03-21 11:04:08 发布