Cod sursa(job #796430)

Utilizator MtkMarianHagrSnaf MtkMarian Data 11 octombrie 2012 15:13:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 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;
		
		
	}
	else
	{
		vt [ a ] = b;		
		
	}
	if ( h [a ] == h[ b ]) ++h [ b ];
}
int  verifica ( int x )
{
	
	int temp = x,rad;
	
	while ( vt [ temp ]!=-1 )  temp =vt [ temp ] ;
		
	rad = temp; 
		
	while ( vt [ x ] != -1 ) 
	{
		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)
	{
		vt[i]=-1;	

	
	}
	
	for( int i=1 ; i <= m ; ++i)
	{
		scanf("%d %d %d",&t,&x,&y);
		x=verifica ( x );
		y= verifica ( y ) ;
		if( t == 1 )			
				 uneste ( x,y );
			
		else 
			if( x == y ) printf( "DA\n" );
			else printf("NU\n");
	}

	return 0;
}