Cod sursa(job #686765)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 21 februarie 2012 20:29:04
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include<cstdio>
#include<set>
#include<ctime>
#include<vector>
#define NMAX 100010

#define INfile "disjoint.in"
#define OUTfile "disjoint.out"

using namespace std ;

int H [ NMAX ] ;

int N , M ;

void solve () ;
void task1 ( int x , int y ) ;
void task2 ( int x , int y ) ;

int main ()
{
	freopen ( INfile , "r" , stdin ) ;
	freopen ( OUTfile , "w" , stdout ) ;
		
	solve () ;

	return 0 ;
}


void solve ()
{
	int ind , cod , x , y , nr = 0 ;
	scanf ( "%d %d" , & N , & M ) ;
	for ( ind = 1 ; ind <= M ; ++ ind )
	{

		scanf ( "%d %d %d" , & cod , & x , & y) ;
		
		if ( cod == 1 ) 
			task1 ( x , y ) ;
		else 
			task2 ( x , y ) ;
	}	
}

void task1 ( int x , int y )
{
	int nrx = 1 , nry = 1 ;
	int xi = x , yi = y ;
	while ( H [ x ] )
	{
		x = H [ x ] ;
		++ nrx ;
	}
	while (  H [ y ] ) 
	{
		y = H [ y ] ;
		++ nry ;
	}
	if ( nrx >= nry )
		H [ y ] = x ;
	else 
		H [ x ] = y ;
}

void task2 ( int x , int y )
{
	while (  H [ x ] )
		x = H [ x ] ;
	while (  H [ y ] )
		y = H [ y ] ;
	if (  x == y ) 
		printf ( "DA\n" ) ;
	else  
		printf ( "NU\n" ) ;
}