Cod sursa(job #704059)

Utilizator rares192Preda Rares Mihai rares192 Data 2 martie 2012 16:10:29
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include<fstream>
using namespace std;

ofstream fout("disjoint.out");

int n, m;
int t[100001], h[1000001];
void Read();
int Find(int );
void Union(int , int);

int main()
{
	Read();
	fout.close();
	return 0;
}

void Read()
{
	ifstream fin("disjoint.in");
	fin >> n >> m;
	
	for(int i = 1; i <= n; ++i)
		t[i] = i;
	
	int x, y, z;
	for(int i = 1; i <= m; ++i)
	{
		fin >> x >> y >> z;
		if( x == 1)
			Union(y, z);
		else 
		{
			int v1 = Find(y);
			int v2 = Find(z);
			if( v1 != v2)
				fout << "NU" << "\n";
			else
				fout << "DA" << "\n";
		}
	}
	fin.close();
}

int Find(int x)
{
	if( t[x] != x)
		t[x] = Find(t[x]);
	return t[x];
}

void Union(int x, int y)
{
	if(h[x] > h[y] )
		t[y] = x;
	else
	{
		t[x] = y;
		if(h[x] == h[y])
			++h[y];
	}
}