Cod sursa(job #863284)

Utilizator alexdmotocMotoc Alexandru alexdmotoc Data 23 ianuarie 2013 18:01:29
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxN 100005

int tata[maxN] , H[maxN];

int root (int x)
{
	int R;
	
	for (R = x ; R != tata[R] ; R = tata[R]);
	for (int y = x ; y != R ; tata[y] = R , y = tata[y]);
	
	return R;
}

void update (int x , int y)
{
	int root_x = root (x);
	int root_y = root (y);
	
	if (H[root_x] > H[root_y])
		tata[root_y] = root_x;
	
	else if (H[root_x] < H[root_y])
		tata[root_x] = root_y;
	
	else
	{
		++H[root_x];
		tata[root_y] = root_x;
	}
}

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