Cod sursa(job #643956)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 4 decembrie 2011 21:04:47
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <cstdio>

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

#define nmax 101000

int N,M,i,tip,x,y,tata[nmax],nr[nmax],t1,t2;

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

void unite(int i, int j){
	
	if (nr[i]>nr[j]){
		tata[j]=i;
		nr[j]+=nr[i];
	}
	else{
		tata[i]=j;
		nr[i]+=nr[j];
	}
}

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