搬运jiangly鸽鸽的板子
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
struct DSU {
std::vector<int> f, siz;
DSU() {}
DSU(int n) {
init(n);
}
void init(int n) {
f.resize(n);
std::iota(f.begin(), f.end(), 0);
siz.assign(n, 1);
}
int find(int x) {
while (x != f[x]) {
x = f[x] = f[f[x]];
}
return x;
}
bool same(int x, int y) {
return find(x) == find(y);
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return false;
}
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x) {
return siz[find(x)];
}
};
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n,m;std::cin >> n >> m;
DSU dsu = DSU(n);
while(m--){
std::string op;int a,b;std::cin >> op;
if(op=="C"){
std::cin >> a >> b;
if(!dsu.same(a,b)) dsu.merge(a,b);
}else if(op=="Q1"){
std::cin >> a >> b;
std::cout << (dsu.same(a,b)?"Yes\n":"No\n");
}else{
std::cin >> a;
std::cout << dsu.size(a) << '\n';
}
}
return 0;
}