Cod sursa(job #263630)

Utilizator tudalexTudorica Constantin Alexandru tudalex Data 20 februarie 2009 18:24:23
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <stdio.h>
#include <stdlib.h>
long *a, *b, i;
long query(long x)
{
	long k = x, p = 0;
	while (a[x]!=-1)
		x = a[x];
	while (a[k]!= x && a[k]!=-1)
	{
		p = k;
		k = a[k];
		a[p] = x;
	}
	return x;
}

        	
	 
int main()
{
	long n, m, i, x, y, c;
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w", stdout);
	scanf("%ld %ld", &n, &m);
	a = new long[n+1];
       //	int *b = new long[n+1];
	for (i = 1; i <= n; ++ i)
		a[i] = -1;
	for (i = 1; i <= m; ++ i)
	{
		scanf("%ld %ld %ld", &c, &x, &y);
		if (c == 1)
		{
			x = query(x);
			y = query(y);
			a[x] = y;
		}
		else
		{
			if (query(x)==query(y))
				printf("DA\n");
			else
				printf("NU\n");
		}
	}
	return 0;
}