Cod sursa(job #499120)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 8 noiembrie 2010 20:09:20
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <stdio.h>

#define Nmax 100001

int N, M, T[Nmax];

int get_root(int x) {
	while(T[x]!=x) 
		x=T[x];
	return x;
}

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

	int i, tip, x, y, root1, root2;
	
	scanf("%d %d",&N,&M);
	for (i=1; i<=N; i++) 
		T[i]=i;
	
	while(M--) {
		scanf("%d %d %d",&tip,&x,&y);
		root1=get_root(x); root2=get_root(y);		
		if(tip==1) {
			if(root1<root2) 
				T[root2]=root1;
			else 
				T[root1]=root2;
		}
		else 
			printf("%s\n", root1==root2 ? "DA" : "NU");
	}
	
	return 0;
}