Cod sursa(job #579169)

Utilizator Cristy94Buleandra Cristian Cristy94 Data 11 aprilie 2011 21:44:45
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include<cstdio>
#include<cstring>
#define Nmax 100010

using namespace std;

int N,M,t[Nmax];

int tata(int x){	
	if(t[x]>0)
		x = tata(t[x]);
	return x;
}

void uneste(int x,int y){
	
	int tx=tata(x);
	int ty=tata(y);
	
	if(t[tx] < t[ty])
		t[tx] += t[ty],
	    t[ty] = tx;
	else 
		t[ty] += t[tx],
	    t[tx] = ty;
	
}

bool conect(int x,int y){
	
	int tx=tata(x);
	int ty=tata(y);
	
	return (tx==ty);
}

int main(){
	
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	
	scanf("%d%d",&N,&M);
	
	memset(t,-1,sizeof(t));
	
	for(int i=1;i<=M;++i){
		
		int tip,x,y;
		scanf("%d%d%d",&tip,&x,&y);
		
		switch(tip){
		    case 1: uneste(x,y);
			        break;
		    case 2: if(conect(x,y))
				       printf("DA\n");
				    else 
					   printf("NU\n");
		}
		
	}
		
		
return 0;
}