Cod sursa(job #293684)

Utilizator petrePajarcu Alexandru-Petrisor petre Data 1 aprilie 2009 23:54:55
Problema Paduri de multimi disjuncte Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.8 kb
var x,gr:Array[1..100010] of longint;
op,n,m,i,j,k,a,b,t,cc,tt:longint;

function caut(var nod:longint):longint;
begin
t:=nod;
while t<>x[t] do t:=x[t];
cc:=t;
t:=nod;
while t<>x[t] do
	begin
	tt:=x[t];
	x[t]:=cc;
	t:=tt;
	end;
caut:=t;
end;

begin

assign(input,'disjoint.in');
reset(input);
assign(output,'disjoint.out');
rewrite(output);

read(n,m);
for i:=1 to n do
	begin
	x[i]:=i;
	gr[i]:=1;
	end;

for i:=1 to m do
	begin
	read(op,a,b);
	if op=1 then

		if caut(a)<>caut(b)
				then
                                begin
				if gr[x[a]]<gr[x[b]] then
							x[x[a]]:=x[b]
							else x[x[b]]:=x[a];
				if gr[x[a]]=gr[x[b]] then inc(gr[b[x]]);
		end
		else
		if caut(a)=caut(b) then writeln('DA')
			else writeln('NU');
	end;
close(input);
close(output);
end.