#include<bits/stdc++.h> using namespace std; const int N=1e4+10; int a[N]; int n,m; int fa[N]; int find(int x){ if(fa[x]==x){ return x; } fa[x]=find(fa[x]); return fa[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++){ int z,x,y; scanf("%d%d%d",&z,&x,&y); if(z==1){ fa[find(x)]=find(y); } else { if(find(x)==find(y)){ printf("Y\n"); } else { printf("N\n"); } } } return 0; }
原文:https://www.cnblogs.com/LightyaChoo/p/13204731.html