Cod sursa(job #582606)

Utilizator skullLepadat Mihai-Alexandru skull Data 15 aprilie 2011 16:40:58
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <stdio.h>
using namespace std;
#define nmax 100005

int GR[nmax];

int grupa ( int x )
{
	if (GR[x] == x) return x;
	GR[x] = grupa(GR[x]);
	return GR[x];
}

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