Cod sursa(job #686680)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 21 februarie 2012 19:31:05
Problema Paduri de multimi disjuncte Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include<cstdio>
#include<set>
#include<ctime>
#include<vector>
#define NMAX 100010

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

using namespace std ;

vector < int > H ( NMAX ) ;

int N , M ;

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

int main ()
{
	freopen ( INfile , "r" , stdin ) ;
	freopen ( OUTfile , "w" , stdout ) ;
	
	//double ss , tt ;
	//ss = clock () ;
	
	solve () ;
	
	//tt = clock () ;
	//printf ( "%lf" , ( tt - ss ) / CLK_TCK ) ; 
	
	return 0 ;
}

void initialise ()
{
	int i ;
	for ( i = 1 ; i <= N ; ++ i )
	{
		H [ i ] = i ;
		//P [ i ].insert ( i ) ;
	}
}

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

		scanf ( "%d %d %d" , & cod , & x , & y) ;
		if ( ind == 24340 )
			cod = cod ;
		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 ( x != H [ x ] )
	{
		x = H [ x ] ;
		++ nrx ;
	}
	while ( y != H [ y ] ) 
	{
		y = H [ y ] ;
		++ nry ;
	}
	if ( nrx >= nry )
		H [ yi ] = x ;
	else 
		H [ xi ] = y ;
	
	/*
	if ( P [ H [ x ] ].size () < P [ H [ y ] ].size () )
	{
		//P [ H [ y ] ].insert ( P [ H [ x ] ] .begin (), P [ H [ x ] ] .end () ) ;
		while ( x != H [ x ] )
			x =
		H [ x ] = H [ y ] ;
	}
	else
	{
		//P [ H [ x ] ].insert ( P [ H [ y ] ] .begin (), P [ H [ y ] ] .end () ) ;
		H [ y ] = H [ x ] ;
	}
	*/
}

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