https://www.luogu.org/problem/P3367
#include<iostream>
using namespace std;
int f[10010];
int find(int x) {
if (x == f[x]) return x; //找到根结点
return f[x] = find(f[x]); //回溯到父结点,同时将父结点和根节点直接连接
}
int main() {
int n, m;
int z, x, y;
cin >> n >> m;
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= m; i++) {
cin >> z >> x >> y;
if (z == 1) {
f[find(x)] = find(y);//把y的根结点连接到x的根结点上
}
else {
if (find(x) == find(y)) cout << "Y\n";
else cout << "N\n";
}
}
return 0;
}