Cod sursa(job #833060)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 11 decembrie 2012 20:52:21
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int T[100010], H[100010];

int find (int x)
{
	if (x == T[x])
		return x;
	
	return find (T[x]);
}

void unite (int x, int y)
{
	if (H[x] == H[y]){
		H[x] ++;
		T[y] = x;
	}
	else
		if (H[x] > H[y])
			T[y] = x;
		else
			T[x] = y;
}

int main ()
{
	int N, M, tx, ty, i, x, y, op;
	
	in >> N >> M;
	
	for (i = 1; i <= N; i ++)
		T[i] = i;
	
	while (M --){
		in >> op >> x >> y;
		tx = find (x);
		ty = find (y);
		
		if (op == 1)
			unite (tx, ty);
		else
			if (tx == ty)
				out << "DA\n";
			else
				out << "NU\n";
	}
	
	return 0;
}