Cod sursa(job #1034828)

Utilizator SzymonSidorSzymonSidor SzymonSidor Data 18 noiembrie 2013 06:43:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <algorithm>
#include <iostream>
#include <fstream>

using namespace std;

int tata[100010];

int inline root(int nod) {
	if (tata[nod] != nod)
		tata[nod] = root(tata[nod]);

	return tata[nod];
}

int main() {
	ifstream cin("disjoint.in");
	ofstream cout("disjoint.out");

	int n, m;
	cin >> n >> m;

	for (int i = 1; i <= n; i++)
		tata[i] = i;

	for (; m; m--) {
		int tip, x, y;
		cin >> tip >> x >> y;

		if (tip == 1) {
			if (root(x) != root(y))
				tata[root(x)] = root(y);
		}
		else cout << ((root(x) == root(y))? "DA\n" : "NU\n");
	}

	return 0;
}