AcWing 836. 合并集合 C++
原题链接
简单
作者:
LaChimere
,
2021-05-21 09:20:18
,
所有人可见
,
阅读 235
C++
#include <iostream>
using namespace std;
const int MAXSIZE = 100010;
int p[MAXSIZE];
void init() {
for (int i = 0; i < MAXSIZE; ++i) {
p[i] = i;
}
}
int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
int main() {
int n, m;
cin >> n >> m;
init();
char op;
int x, y;
while (m--) {
cin >> op >> x >> y;
if (op == 'M') {
p[find(x)] = find(y);
}
if (op == 'Q') {
if (find(x) == find(y)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
return 0;
}