Cod sursa(job #1366683)

Utilizator veleanduAlex Velea veleandu Data 1 martie 2015 12:55:38
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
using namespace std;

const int kMaxN = 100005;

ifstream in;
ofstream out;

int father[kMaxN], deep[kMaxN];

void unite(int a, int b) {
	if (deep[a] > deep[b]) {
		father[b] = a;
	} else {
		deep[b] = max(deep[b], deep[a] + 1);
		father[a] = b;
	}
}

int getFather(int nod) {
	if (father[nod] != nod)
		father[nod] = getFather(father[nod]);
	return father[nod];
}

int main() {
	in.open("disjoint.in");
	int n, m; in >> n >> m;
	for (int i = 1; i <= n; ++i) {
		father[i] = i;
		deep[i] = i;
	}

	out.open("disjoint.out");
	while (m--) {
		int t; in >> t;
		if (t == 1) {
			int x, y; in >> x >> y;
			unite(getFather(x), getFather(y));
		} else {
			int x, y; in >> x >> y;
			out << ((getFather(x) == getFather(y)) ? ("DA") : ("NU")) << '\n';
		}
	}

	in.close();
	out.close();
	return 0;
}