Cod sursa(job #542175)

Utilizator feelshiftFeelshift feelshift Data 25 februarie 2011 21:33:11
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
// http://infoarena.ro/problema/disjoint
#include <fstream>
using namespace std;

#define maxSize 100002

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

int number,operations,type,one,two;
int father[maxSize];

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

int main() {
	in >> number >>  operations;

	for(int i=1;i<=number;i++)
		father[i] = i;
	
	for(int i=1;i<=operations;i++) {
		in >> type >> one >> two;

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

	return (0);
}

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

int find(int node) {
	int i;
	for(i=node;father[i]!=i;i=father[i]);

	return i;
}