62 lines
1.0 KiB
C++
62 lines
1.0 KiB
C++
|
#include <cctype>
|
||
|
#include <cstdio>
|
||
|
#include <iostream>
|
||
|
using namespace std;
|
||
|
|
||
|
const int MAX_N=1e4+114;
|
||
|
int n,m;
|
||
|
int b[MAX_N];
|
||
|
|
||
|
inline int readint();
|
||
|
inline int getfather(int n);
|
||
|
inline int getroot(int n);
|
||
|
inline void pre(int n);
|
||
|
|
||
|
#define PV(v){cout<<#v<<" : "<<(v)<<endl;}
|
||
|
#define L(m){cout<<"[LOG]: "<<(m)<<endl;}
|
||
|
|
||
|
int main(){
|
||
|
n=readint(),m=readint();
|
||
|
pre(n);
|
||
|
for(int i=1;i<=m;i++){
|
||
|
const int z=readint(),x=readint(),y=readint();
|
||
|
if(z==1){
|
||
|
b[getroot(x)]=getroot(y);
|
||
|
}else{
|
||
|
|
||
|
cout<<(getroot(x)==getroot(y)?"Y":"N")<<endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void pre(int n){
|
||
|
for(int i=1;i<=n;i++){
|
||
|
b[i]=i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int getroot(int n){
|
||
|
int r=getfather(n);
|
||
|
while(r!=getfather(r)){
|
||
|
r=b[r]=b[b[r]];
|
||
|
}
|
||
|
return r;
|
||
|
}
|
||
|
|
||
|
int getfather(int n){
|
||
|
return b[n];
|
||
|
}
|
||
|
|
||
|
int readint(){
|
||
|
int x=0,w=1;
|
||
|
char ch=0;
|
||
|
while (!isdigit(ch)) {
|
||
|
if(ch=='-')w=-1;
|
||
|
ch=getchar();
|
||
|
}
|
||
|
while(isdigit(ch)){
|
||
|
x=x*10+(ch-'0');
|
||
|
ch=getchar();
|
||
|
}
|
||
|
return x*w;
|
||
|
}
|