Cod sursa(job #1128320)

Utilizator superman_01Avramescu Cristian superman_01 Data 27 februarie 2014 16:31:02
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

#define NMAX 100005
#define get_max(a,b) ((a)>(b)?(a):(b))
#define get_min(a,b) ((a)<(b)?(a):(b))

using namespace std;

ifstream in ( "disjoint.in" );
ofstream out ( "disjoint.out" );

int TT[NMAX] , RG[NMAX] , N , M ;

int Find ( int X )
{
	int R , y;
	for ( R = X ; R != TT[R] ; R = TT[R] );
	for ( ; X != TT[X] ; )
	{
		y = TT[X] ;
		TT[X] = R ;
		X = y ;
	}
	return R;
}
bool Unite ( int x , int y )
{
	if ( RG[x] > RG[y] )
		TT[y] = x ;
	else TT[x] = y ;
	if ( RG[x] == RG[y] ) ++RG[y];	
}
int main ( void )
{
	int i , j , x , y , type ;
	in >> N >> M ;
	for ( i = 1 ; i <= N ; TT[i++] = i );
	for ( i = 1 ; i <= M ; ++i )
	{
		in >> type >> x >> y ;
	   ( type == 1 ? Unite ( Find(x) , Find(y)) : ( Find(x) != Find(y) ? out << "NU\n" : out <<"DA\n") );
	}
	in.close();
	out.close();
	return 0 ;
}