Cod sursa(job #742471)

Utilizator andreipasalauPasalau Andrei andreipasalau Data 30 aprilie 2012 14:00:54
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <math.h>
#include<fstream>

using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

long n, m, i, a, b, c, tata[100010], aux, nivel[100010], max;

long parc(long nod, long o) {
	if (aux != -1)
		if (o == 1) tata[nod] = aux;
	if (nivel[nod] > max) max = nivel[nod];
	if (tata[nod]) parc(tata[nod], o);
	else {
		if (aux == -1) {
			aux = nod; 
			nivel[nod] = max;
		}
		return aux;
	}
}

int main() {
	freopen("disjoint.in", "r", stdin);
	freopen("disjoint.out", "w", stdout);
	
	f >> n >> m;
	for (i = 1; i <= m; ++i) {
		f >> c >> a >> b;
		if (c == 1) {
			aux = -1;max = 0;
			long A = parc(a, 0);
			aux = -1;max = 0;
			long B = parc(b, 0);
			if (nivel[A] < nivel[B]) {
				tata[A] = B;
				nivel[A] = nivel[B] + 1;
			} else {
				tata[B] = A;
				nivel[B] = nivel[A] + 1;
			}
		}
		if (c == 2) {
			aux = -1;max = 0;
			long u = parc(a, 0);
			aux = -1;max = 0;
			long y = parc(b, 0);
			if (u == y) {
				g << "DA\n";
			} else {
				g << "NU\n";
			}
			aux = -1;max = 0;
			if (tata[a]) parc(a, 1);
			aux = -1;max = 0;
			if (tata[b]) parc(b, 1);			
		}
	}
	return 0;
}