洛谷3367
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int maxn=10010; 5 int n,m,x,y,k,fa[maxn]; 6 void read(int &k){ 7 k=0; int f=1; char c=getchar(); 8 while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 9 while ('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 10 k*=f; 11 } 12 int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} 13 int main(){ 14 read(n); read(m); for (int i=1;i<=n;i++) fa[i]=i; 15 for (int i=1;i<=m;i++){ 16 read(k); read(x); read(y); 17 if (k==1) fa[find(x)]=find(y); 18 else printf(find(x)==find(y)?"Y ":"N "); 19 } 20 return 0; 21 }