Cod sursa(job #293608)

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

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 gr[a]>gr[b] then
				x[b]:=a
				else x[a]:=b;
		if gr[a]=gr[b] then gr[b]:=gr[b]+1;
		end
		else
		begin
		t:=a;
		while x[t]<>t do t:=x[t];
		tt:=a;
		while x[tt]<>tt do begin
							cc:=x[tt];
							x[tt]:=t;
							tt:=cc;
							end;
		t:=b;
		while x[t]<>t do t:=x[t];
                if t=x[a] then writeln('DA')
					else writeln('NU');
		tt:=b;
		while x[tt]<>tt do
			begin
			cc:=x[tt];
			x[tt]:=t;
			tt:=cc;
			end;
		end;

	end;
close(input);
close(output);
end.