Cod sursa(job #542621)

Utilizator feelshiftFeelshift feelshift Data 26 februarie 2011 18:06:32
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
// http://infoarena.ro/problema/disjoint
#include <fstream>
using namespace std;

#define maxSize 100001

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

int father[maxSize];

void unite(int first,int second);
int find(int node);

int main() {
	int number,operations;
	int type,first,second;

	in >> number >> operations;

	// initial fiecare nod este izolat
	for(int i=1;i<=number;i++)
		father[i] = i;

	for(int i=1;i<=operations;i++) {
		in >> type >> first >> second;

		switch(type) {
			case 1:
				unite(find(first),find(second));
				break;
			case 2:
				if(find(first) == find(second))
					out << "DA\n";
				else
					out << "NU\n";
				break;
		}
	}

	in.close();
	out.close();

	return (0);
}

void unite(int first,int second) {
	father[first] = second;
}

int find(int node) {
	/*if(father[node] == node)
		return node;
	else
		return find(father[node]);*/

	while(father[node] != node)
		node = father[node];

	return node;
}