Cod sursa(job #896018)

Utilizator The_DisturbedBungiu Alexandru The_Disturbed Data 27 februarie 2013 13:28:27
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include<cstdio>
int t[100013],m,n,i,c,x,y;
inline void uneste(int x, int y)
{
	int i,j;
	i=j=0;
	while(x!=t[x]){x=t[x];++i;}
	while(y!=t[y]){y=t[y];++j;}
	if(i<j)t[x]=y; else t[y]=x;
}
inline void gaseste(int x, int y)
{
	while(x!=t[x])x=t[x];
	while(y!=t[y])y=t[y];
	if(x==y)printf("DA\n");else printf("NU\n");
}
int main()
{
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;++i)t[i]=i;
	for(i=0;i<m;++i)
	{
		scanf("%d%d%d",&c,&x,&y);
		switch(c)
		{
		case 1:
			{
				uneste(x,y);
				break;
			}
		case 2:
			{
				gaseste(x,y);
				break;
			}
		}
	}
	return 0;
}