Cod sursa(job #293674)

Utilizator petrePajarcu Alexandru-Petrisor petre Data 1 aprilie 2009 23:42:38
Problema Paduri de multimi disjuncte Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 0.74 kb
var x,gr:Array[1..100005] 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
		begin
		if caut(a)<>caut(b)
				then
				if gr[a]<gr[b] then
							x[a]:=b
							else x[b]:=a;
				if gr[a]=gr[b] then inc(gr[b]);
		end
		else
		if caut(a)=caut(b) then writeln('DA')
			else writeln('NU');
	end;
close(input);
close(output);
end.