1. 程式人生 > >【洛谷】P3367 【模板】並查集

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

space ret class cpp else if 並查集 scan == namespace

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

  

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