Cod sursa(job #1533380)

Utilizator greenadexIulia Harasim greenadex Data 22 noiembrie 2015 14:40:16
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>

using namespace std;

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

const int MAX = 1e5 + 5;

void unite(int father[], int a, int b) {
	father[a] = b;
}

int find_root(int father[], int a) {
	while (father[a] != a)
		return find_root(father, father[a]);
	return father[a];
}

int main() {
	int length, queries;
	int father[MAX];
	
	fin >> length >> queries;
	
	for (int i = 1; i <= length; i++)
		father[i] = i;
	
	for (int query, a, b; queries; queries--) {
		fin >> query >> a >> b;
		if (query == 1)
			unite(father, find_root(father, a), find_root(father, b));
		else fout << (find_root(father, a) == find_root(father, b) ? "DA\n" : "NU\n");
	}		
	return 0;
}