Cod sursa(job #796422)

Utilizator MtkMarianHagrSnaf MtkMarian Data 11 octombrie 2012 14:44:01
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include<cstdio>
#include<string.h>
using namespace std;
#define NMAX  100005
int n,m,x,y,t;
int vt[NMAX],h[NMAX];
void uneste ( int a , int b )
{
	if ( h[ a ] >= h[ b ] ) 
	{
		vt [ b ] = a;
		
		if ( h [ b ] + 1 >h [ a ] ) ++h [ a ] ;
	}
	else
	{
		vt [ a ] = b;		
		if ( h [ a ] + 1 >h [ b ] ) ++h [ b ] ;
	}
}
int  verifica ( int x )
{
	
	int temp = x,rad;
	
	while ( vt [ temp ]!=0 )  temp =vt [ temp ] ;
		
	rad = temp; 
		
	while ( vt [ x ] != 0 ) 
	{
		temp = x ;
		x = vt [ x ];
		vt [ temp ] = rad ;		
	}

	return rad ;
}
		
int main()
{
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	scanf("%d %d",&n,&m);
	
	for(int i=1;i<=n;++i)
	{
		h[i]=1;
		vt[i]=0;		
	
	}
	
	


	for( int i=1 ; i <= m ; ++i)
	{
		scanf("%d %d %d",&t,&x,&y);

		if( t == 1 ) uneste ( x,y );
		else 
			if( verifica( x ) == verifica( y ) ) printf( "DA\n" );
			else printf("NU\n");
	}

	return 0;
}