【模板】并查集 - 洛谷
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+9;
int a[N];
int Find(int x)
{if(x==a[x]){return x;}else{a[x]=Find(a[x]);return a[x];}
}
void push(int x,int y)
{a[Find(x)]=Find(y);return ;
}
int main()
{int n,m; cin>>n>>m;for(int i=1;i<=n;i++){a[i]=i;}for(int i=1;i<=m;i++){int op,x,y; cin>>op>>x>>y;if(op==1) //连接{push(x,y);}else{if(Find(x)==Find(y)){cout<<"Y"<<endl;}else cout<<"N"<<endl;}}return 0;
}