Cod sursa(job #266847)

Utilizator peanutzAndrei Homorodean peanutz Data 26 februarie 2009 10:32:41
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <stdio.h>

#define NMAX 100010

long n, m;
long a[NMAX];

int find(int x)
{
	if(a[x] == x) return x;
	x = find(a[x]);
	a[x] = x;
	return x;
}

void join(int x, int y)
{
	if(x&1)
		a[x] = y;
	else
		a[y] = x;
}

int main()
{
	long i;
	long o, x, y;
	freopen("disjoint.in", "r", stdin);
	freopen("disjoint.out", "w", stdout);

	scanf("%ld %ld", &n, &m);
	for(i = 1; i <= n; ++i) a[i] = i;

	while(m--)
	{
		scanf("%ld %ld %ld", &o, &x, &y);
		if(o == 1)
		{
			x = find(x);
			y = find(y);
			join(x, y);
		}
		else
		{
			x = find(x);
			y = find(y);
			if(x == y)
				printf("DA\n");
			else
				printf("NU\n");
		}
	}
	return 0;
}