Cod sursa(job #718133)

Utilizator mening12001Andrei Geogescu mening12001 Data 20 martie 2012 16:07:30
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ifstream f("disjoint.in");
ofstream h("disjoint.out");
int n,m,t[100000],r[100000],i,o,x,y,b,a;
f>>n>>m;
for(i=1;i<=n;i++)
{t[i]=i;
r[i]=1;}
for(i=1;i<=m;i++)
	{f>>o>>a>>b;
if(o==1)
{	x=a;
		while(t[x]!=x)
			x=t[x];
		y=b;
		while(t[y]!=y)
			y=t[y];
if(r[x]>r[y])
t[y]=x;
else
	if(r[x]==r[y])
		{t[y]=x;
	r[y]++;}
		else
			t[x]=y;}
else
	{x=a;
		while(t[x]!=x)
			x=t[x];
		y=b;
		while(t[y]!=y)
			y=t[y];
		if(x!=y)
			h<<"NU"<<'\n';
		else
			h<<"DA"<<'\n';}	}
		return 0;}