rt 代码如下(带路径压缩)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5;
int n,m;
int s[N];
void init(){
for(int i=1;i<=n;i++) s[i]=i;
}
int find_set(int x){
return x==s[x]?x:s[x]=find_set(s[x]);
}
signed main(){
cin>>n>>m;
init();
while(m--){
int x,y,z;
cin>>z>>x>>y;
int xx=find_set(x),yy=find_set(y);
if(z==1) s[xx]=yy;
else if(z==2) cout<<(xx==yy?'Y':'N')<<endl;
}
return 0;
}