Cod sursa(job #547293)

Utilizator andrei.dAndrei Diaconeasa andrei.d Data 6 martie 2011 10:52:16
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <cstdio>

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

int i,x,y,t1,t2,tip,tata[101000],n,m;

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

void unite(int i, int j){
	
	tata[i]=j;
}

int main(){
	
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d %d", &n, &m);
	
	for (i=1;i<=n;++i) tata[i]=i;
	
	while(m--){
		
		scanf("%d %d %d", &tip, &x, &y);
		
		t1=father(x);
		t2=father(y);
		
		if (tip==1){
			
			unite(t1,t2);
		}
		else{
			
			if (t1==t2)
				printf("DA\n");
			else
				printf("NU\n");
		}
	}
	
	return 0;
	
}