Cod sursa(job #266216)

Utilizator MarquiseMarquise Marquise Data 25 februarie 2009 08:24:47
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <stdio.h>

#define NMAX 100001

int h[NMAX], t[NMAX], N, M;

int tata(int nod)
{
	if ( nod != t[nod])
		t[nod] = tata( t[nod] );

	return  t[nod];
}


int main()
{
	int m, x, y, cod, i, tx, ty;

	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 ( m = 1; m <= M; m++)
	{
		scanf("%d %d %d", &cod, &x, &y);

		tx = tata(x);
		ty = tata(y);

		if (cod == 1)
		{

			if ( h[tx] > h[ty])
				t[ty] = tx;
			else
				t[tx] = ty;

			if ( h[tx] == h[ty] )
				h[ty]++;
		}
		else
		{
			if ( tx == ty)
				printf("DA\n");
			else
				printf("NU\n");
		}
	}

	return 0;
}