并查集
#include
using namespace std;
#define LL long long
const int N = 1e4 + 10;
LL n, m, z, x, y;
vector fa(N);
LL find(LL x){
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
void join(LL a, LL b){
fa[find(b)] = find(a);
}
void solve(){
cin >> n >> m;
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++){
scanf("%lld%lld%lld", &z, &x, &y);
if (z == 1)
join(x, y);
else
if (find(x) == find(y))
cout << "Y\n";
else
cout << "N\n";
}
}
int main(){
solve();
return 0;
}
模板:https://www.luogu.com.cn/problem/P3367