Cod sursa(job #832936)

Utilizator EduardGeorgescuGeorgescu Eduard EduardGeorgescu Data 11 decembrie 2012 18:31:44
Problema Paduri de multimi disjuncte Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include<cstdio>

using namespace std ;

int h[100],rad[101];

int find ( int x ) {

	if(rad[x]==x)
		return x;
	return find(rad[x]);

}

void Union ( int x , int y ) {

	if( h[x]==h[y] ) {
		h[x]++;
		rad[y]=x;
	}
	else
		if ( h[x] > h[y] )
			rad[y] =x;
		else
			rad[x]=y;
}

int main(){

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

	int n,m,x,y,tx,ty,i,c;

	scanf("%d" , &n);

	for(i=1;i<=n;i++)
		rad[i]=i;

	scanf("%d" , &m);
	for(i=1;i<=m;i++){
	    scanf("%d" , &c );
        scanf("%d%d" , &x , &y );
        if ( c==1 ){
		tx=find(x);
		ty=find(y);
        Union(tx,ty);
        }
        else
            if(find(x)==find(y))
                printf("DA\n");
            else
                printf("NU\n");
	}


	return 0;
}