【洛谷】P3367 【模板】并查集

#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;
}

  

posted @ 2017-11-30 21:56  surpassion  阅读(203)  评论(0编辑  收藏  举报