链接https://www.luogu.org/problem/P3367
#include<bits/stdc++.h> using namespace std; const int range=10005; int fa[range]; int find(int x) { if(fa[x]!=x) fa[x]=find(fa[x]); return fa[x]; } int main() { int n,m; cin>>n>>m; for(int i=1; i<=n; i++) { fa[i]=i; } while(m--) { int a,x,y; cin>>a>>x>>y; if(a==1) { fa[find(x)]=find(y); } else if(a==2) { if(find(x)==find(y)) printf("Y "); else printf("N "); } } return 0; }
模板