Cod sursa(job #832736)

Utilizator antonioteoZait Teodor Antonio antonioteo Data 11 decembrie 2012 11:38:46
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
#include <cstring>
using namespace std;
const char iname[] = "disjoint.in";
const char oname[] = "disjoint.out";
ifstream fin(iname);
ofstream fout(oname);
int comp[ 100010 ];
inline int cauta ( int x )
{
	int r , y;
	r = x;
	while ( comp[ r ] > 0 )
		r = comp[ r ];
	while ( comp[ x ] > 0 )
	{
		y = comp[ x ];
		comp[ x ] = r;
		x = y;
	}
	return r;
}
inline void uneste ( int x , int y )
{
	if ( comp[ x ] > comp[ y ] )
	{
		comp[ y ] += comp[ x ];
		comp[ x ] = y;
	}
	else
	{
		comp[ x ] += comp[ y ];
		comp[ y ] = x;
	}
}
int main()
{
	int n , m , x , y , cod , i;
	fin >> n >> m;
	memset ( comp , -1 , sizeof( comp ) );
	for ( i = 1; i <= m; ++i )
	{
		fin >> cod >> x >> y;
		if ( cod == 2 )
		{
			if ( cauta( x ) == cauta( y ) )
				fout << "DA\n";
			else
				fout << "NU\n";
		}
		else
			uneste ( cauta( x ) , cauta( y ) );
	}
	return 0;
}