P3367 【模板】并查集
程序员文章站
2022-06-02 19:54:50
...
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 1 << 20;
int fa[SIZE];
int get(int x) {
if (x == fa[x]) return x;
return fa[x] = get(fa[x]);
}
void merge(int x,int y) {
fa[get(x)] = get(y);
}
int main() {
int n, m; cin >> n >> m;
for (int i = 1; i <= n; ++i) {
fa[i] = i;
}
for (int i = 1; i <= m; ++i) {
int zi, xi, yi; cin >> zi >> xi >> yi;
if (zi == 1) {
merge(xi, yi);
} else {
if (get(xi) == get(yi)) puts("Y");
else puts("N");
}
}
return 0;
}
/*
4 7
2 1 2
1 1 2
2 1 2
1 3 4
2 1 4
1 2 3
2 1 4
*/