Cod sursa(job #477986)

Utilizator mottyMatei-Dan Epure motty Data 16 august 2010 22:51:01
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<cstdio>
#include<vector>

using namespace std;

const int N=100001;

int n, rang[N], root[N];

int GetRoot(int i)
{
	if(root[i]!=i)
		root[i]=GetRoot(root[i]);
	return root[i];
}

void Read()
{
	int m, type, a, b, ra, rb;
	scanf( "%d%d", &n, &m);
	
	for( int i=1; i<=n; ++i)
		rang[i]=1, root[i]=i;
	
	while(m--)
	{
		scanf( "%d%d%d", &type, &a, &b);
		if(type==1)
		{
			ra=GetRoot(a);
			rb=GetRoot(b);
			
			if(ra!=rb)
			{
				if(rang[ra]<rang[rb])
				{
					root[ra]=rb;
					rang[rb]+=rang[ra];
				}
				else
				{
					root[rb]=ra;
					rang[ra]+=rang[rb];
				}
			}
		}
		else
		{
			ra=GetRoot(a);
			rb=GetRoot(b);
			if(ra==rb)
				printf("DA\n");
			else
				printf("NU\n");
		}
	}
	
}

int main()
{
	freopen( "disjoint.in", "r", stdin);
	freopen( "disjoint.out", "w", stdout);
	
	Read();
	
	return 0;
}