You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
833 B
37 lines
833 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 100010;
|
|
int n, m;
|
|
int p[N];
|
|
int s[N];
|
|
int find(int x) {
|
|
if (p[x] != x) p[x] = find(p[x]);
|
|
return p[x];
|
|
}
|
|
int main() {
|
|
cin >> n >> m;
|
|
for (int i = 1; i <= n; i++) p[i] = i, s[i] = 1;
|
|
|
|
while (m--) {
|
|
string op;
|
|
int a, b;
|
|
cin >> op;
|
|
if (op == "C") {
|
|
cin >> a >> b;
|
|
if (find(a) != find(b)) {
|
|
s[find(b)] += s[find(a)];
|
|
p[find(a)] = find(b);
|
|
}
|
|
} else if (op == "Q1") {
|
|
cin >> a >> b;
|
|
if (find(a) == find(b))
|
|
puts("Yes");
|
|
else
|
|
puts("No");
|
|
} else {
|
|
cin >> a;
|
|
printf("%d\n", s[find(a)]);
|
|
}
|
|
}
|
|
return 0;
|
|
} |