Link
解题思路
真真就是个并查集模板
Code
#include <iostream>
#include <cstdio>
using namespace std;
int n, m, fa[10010], c, x, y, xx, yy;
int find(int x) {
if(fa[x] != x) {
fa[x] = find(fa[x]);
return fa[x];
}
return x;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &c, &x, &y);
if(c == 1) {
xx = find(x), yy = find(y);
if(xx != yy)
fa[xx] = yy;
} else {
xx = find(x), yy = find(y);
if(xx != yy) printf("N\n");
else printf("Y\n");
}
}
}