并查集-连通块
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int N = 100010;
string opt;
int a, b;
int n, m;
int fa[N];
vector siz(N, 1);
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
void merge(int x, int y){
x = find(x);
y = find(y);
if (x == y) return;
if (siz[x] > siz[y]) std::swap(x, y);
fa[x] = y;
siz[y] += siz[x];
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) {
fa[i] = i;
siz[i] = 1;
}
while(m -- ) {
cin >> opt;
if (opt == "C") {
cin >> a >> b;
merge(a, b);
}
if (opt == "Q1") {
cin >> a >> b;
if (find(a) == find(b)) cout << "Yes" << endl;
else cout << "No" << endl;
}
if (opt == "Q2") {
cin >> a;
cout << siz[find(a)] << endl;
}
}
}