Cod sursa(job #494366)

Utilizator ChallengeMurtaza Alexandru Challenge Data 21 octombrie 2010 15:01:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>

using namespace std;

const char InFile[]="disjoint.in";
const char OutFile[]="disjoint.out";
const int MaxN=100050;

ifstream fin(InFile);
ofstream fout(OutFile);

int n,m,op,x,y,t[MaxN];

int root(int nod)
{
	int fnod=nod;
	while(t[nod]>0)
	{
		nod=t[nod];
	}
	while(t[fnod]>0)
	{
		t[fnod]=nod;
		fnod=t[fnod];
	}
	return nod;
}

int main()
{
	fin>>n>>m;
	for(register int i=1;i<=n;++i)
	{
		t[i]=-1;
	}

	for(register int i=1;i<=m;++i)
	{
		fin>>op>>x>>y;
		if(op==1)
		{
			int ra=root(x);
			int rb=root(y);
			if(t[ra]>t[rb])
			{
				swap(ra,rb);
			}
			t[ra]+=t[rb];
			t[rb]=ra;
		}
		else
		{
			if(root(x)==root(y))
			{
				fout<<"DA\n";
			}
			else
			{
				fout<<"NU\n";
			}
		}
	}
	fin.close();
	fout.close();
	return 0;
}