Cod sursa(job #462884)

Utilizator andrei.dAndrei Diaconeasa andrei.d Data 13 iunie 2010 23:06:16
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <cstdio>

#define file_in "disjoint.in"
#define file_out "disjoint.out"

#define nmax 101000

int n,m;

int tata[nmax];
int nr[nmax];

void citire()
{
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d %d", &n, &m);
}

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

void unite(int x, int y)
{
	if (nr[x]>nr[y])
	{
		nr[x]+=nr[y];
		tata[y]=x;
	}
	else
	{
		nr[y]+=nr[x];
		tata[z]=y;
	}
}

void solve()
{
	int i,tip,x,y,t1,t2;;
	
	for (i=1;i<=n;++i)
	{
		tata[i]=i;
		nr[i]=1;
	}
	
	while(m--)
	{
		scanf("%d %d %d", &tip, &x, &y);
		
		t1=find(x);
		t2=find(y);
		
		if (tip==1)
			unite(t1,t2);
		else
		{
			if (t1==t2)
				printf("DA\n");
			else
				printf("NU\n");
		}
	}
}

int main()
{
	citire();
	solve();
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
	
}