Cod sursa(job #749011)

Utilizator fhandreiAndrei Hareza fhandrei Data 15 mai 2012 16:36:48
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
//Include
#include <fstream>
using namespace std;

//Constante
const int MAX_SIZE = (int)1e5+1;

//Functii
int getRoot(int node);

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

int elements, questions;
int node1, node2;
int type;
int root[MAX_SIZE];

//Main
int main()
{
	in >> elements >> questions;
	
	while(questions--)
	{
		in >> type >> node1 >> node2;
		if(type == 1)
			root[node2] = getRoot(node1);
		else
			out << (getRoot(node1) == getRoot(node2)? "DA" : "NU") << '\n';
	}
	
	in.close();
	out.close();
	return 0;
}

int getRoot(int node)
{	return root[node]? root[node]=getRoot(root[node]) : node;	}