Cod sursa(job #769783)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 20 iulie 2012 19:59:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>

using namespace std;

int T[100010];

int t, x, y, rx, ry, N, Q;

int rad(int x) { //radacina arborelui in care se afla nodul x
	while (T[x] > 0) {
		x = T[x];
	}
	return x;
}


int main() {
	ifstream f("disjoint.in");
	ofstream g("disjoint.out");
	f>>N>>Q;
	for (;Q;Q--) {
		f>>t>>x>>y;
		if (t==1) {
			rx = rad(x);
			ry = rad(y);
			if (T[rx] < T[ry]) {
				T[rx] += T[ry];
				T[ry] = rx;
			} else {
				T[ry] += T[rx];
				T[rx] = ry;
			}
		} else {
			rx = rad(x);
			ry = rad(y);
			if (rx == ry)
				g<<"DA\n";
			else
				g<<"NU\n";
		}
	}
	return 0;
}