mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 13:58:48 +00:00
836. 合并集合
https://www.acwing.com/problem/content/submission/code_detail/7683086/
This commit is contained in:
parent
d08421fad2
commit
b85894c1ec
26
AcWing/836/836.cpp
Normal file
26
AcWing/836/836.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int n, m, a, b, fa[100005];
|
||||
char op;
|
||||
|
||||
int getfa(int x) {
|
||||
return fa[x] == x ? fa[x] : fa[x] = getfa(fa[x]);
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
fa[i] = i;
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
cin >> op >> a >> b;
|
||||
if (op == 'M') {
|
||||
fa[getfa(a)] = getfa(b);
|
||||
} else {
|
||||
cout << (getfa(a) == getfa(b) ? "Yes" : "No") << endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user