'commit'
This commit is contained in:
32
TangDou/AcWing/DisjointSet/836.cpp
Normal file
32
TangDou/AcWing/DisjointSet/836.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user