Cod sursa(job #1286290)

Utilizator deneoAdrian Craciun deneo Data 7 decembrie 2014 12:22:50
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
// I WAS LOOKING AT ALL THE LIFE. 
// THERE WERE PLANTS AND BIRDS AND ROCKS AND THINGS.

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <map>

using namespace std;

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

int head[100000 + 10], rang[100000 + 10];

int find (int nod) {
	int r, y;

	for (r = head[nod]; r != head[r]; r = head[r]);

	while (head[nod] != r) {
		y = head[nod];
		head[nod] = r;
		nod = y;
	}

	return r;
}

void unite (int a, int b) {
	if (rang[a] >= rang[b]) 
		head[b] = head[a];
	else
		head[a] = head[b];

	if (rang[a] == rang[b])
		++rang[a];
}

int n, m;

int main() {
	fin >> n >> m;

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

	for (int i = 1; i <= m; ++i) {
		int t, x, y;
		fin >> t >> x >> y;
		if (t == 2) 
			fout << (find(x) == find(y) ? "DA" : "NU") << "\n";
		else
			unite(find(x), find(y));
	}

	return 0;
}