Cod sursa(job #360378)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 31 octombrie 2009 11:59:27
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <cstdio>
#define lm 100010

int t[lm], r[lm], n, m;

int find (int x)
{
	int i, c;
	for (i=x; t[i]!=i; i=t[i]);
	for (; t[x]!=x;)
	{
		c=t[x];
		t[x]=i;
		x=c;
	}
	return i;
}

void unite (int x, int y)
{
	if (r[x]<r[y])
		t[x]=y; 
	else t[y]=x;
	if (r[x]==r[y]) r[x]++;
}

int main()
{
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	scanf("%d %d",&n,&m);
	int i, cod, x, y;
	for (i=1; i<=n; i++)
	{
		t[i]=i;
		r[i]=1;
	}
	while (m--)
	{
		scanf("%d %d %d",&cod,&x,&y);
		if (cod==1)
			unite(find(x), find(y)); else
			if (find(x)==find(y)) printf("DA\n"); else printf("NU\n");
	}	
}