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.

32 lines
637 B

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m;
int p[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; // 自己是自己的祖先
while (m--) {
char op;
int a, b;
cin >> op >> a >> b;
if (op == 'M')
p[find(a)] = find(b); // a家族加入b家族
else {
if (find(a) == find(b))
puts("Yes");
else
puts("No");
}
}
return 0;
}