Cod sursa(job #686621)

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

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

using namespace std ;

vector < set < int > > P ( NMAX ) ;
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 i , cod , x , y;
	scanf ( "%d %d" , & N , & M ) ;
	initialise () ;
	for ( i = 1 ; i <= M ; ++ i )
	{
		scanf ( "%d %d %d" , & cod , & x , & y) ;
		if ( cod == 1 ) 
			task1 ( x , y ) ;
		else 
			task2 ( x , y ) ;
	}
}

void task1 ( int x , int y )
{
	if ( P [ H [ x ] ].size () < P [ H [ y ] ].size () )
	{
		P [ H [ y ] ].insert ( P [ H [ x ] ] .begin (), P [ H [ x ] ] .end () ) ;
		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" ) ;
}