连通块的个数
作者:
zplzh
,
2024-03-09 11:43:15
,
所有人可见
,
阅读 27
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100010;
int n, m;
int p[N], cnt[N];
int find(int x) {
if (p[x] == x) return p[x];
else return p[x] = find(p[x]);
}
void un(int a, int b) {
int x = find(a);
int y = find(b);
if (x != y) {
p[x] = y;
cnt[y] += cnt[x];
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
p[i] = i;
cnt[i] = 1;
}
while (m--) {
char op[2];
int a, b;
cin >> op;
if (op[0] == 'C') {
cin >> a >> b;
un(a, b);
} else if (op[1] == '1') {
cin >> a >> b;
if (find(a) == find(b)) {
puts("Yes");
} else {
puts("No");
}
} else {
cin >> a;
cout << cnt[find(a)] << endl;
}
}
return 0;
}