Cod sursa(job #742469)

Utilizator andreipasalauPasalau Andrei andreipasalau Data 30 aprilie 2012 13:59:32
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 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;

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

int main() {
	
	
	f >> n >> m;
	for (i = 1; i <= m; ++i) {
		f >> c >> a >> b;
		if (c == 1) {
			aux = -1;
			long A = parc(a, 0);
			long B = parc(b, 0);
			tata[B] = A;
		}
		if (c == 2) {
			aux = -1;
			long u = parc(a, 0);
			long y = parc(b, 0);
			if (u == y) {
				g << "DA\n";
			} else {
				g << "NU\n";
			}
			if (tata[a]) parc(a, 1);
			if (tata[b]) parc(b, 1);			
		}
	}
	return 0;
}