#include<bits/stdc++.h> using namespace std; int fa[10005],n,m,type,a,b; int father(int x) { if (fa[x] == x) return x; fa[x] = father(fa[x]); return fa[x]; } int main() { scanf("%d %d",&n,&m); for (int i = 1 ; i <= n ; i ++) fa[i] = i; for (int i = 1 ; i <= m ; i ++) { scanf("%d %d %d",&type,&a,&b); if (type == 1) { int u = father(a) , v = father(b); fa[u] = v; } else if (father(a) == father(b)) cout<<"Y"<<endl; else cout<<"N"<<endl; } return 0; }
原文:http://www.cnblogs.com/YMY666/p/7932075.html