Pagini recente » Cod sursa (job #1713894) | Cod sursa (job #863049) | Cod sursa (job #1087967) | Cod sursa (job #2190783) | Cod sursa (job #381563)
Cod sursa(job #381563)
{paduri de multimi disjuncte}
var h,tata:array[1..100001] of longint;
n,m:longint;
procedure reuniune(x,y:longint);
begin
if h[x]>h[y] then tata[y]:=x
else tata[x]:=y;
if h[x]=h[y] then inc(h[y]);
end;
function multime(x:longint):longint;
var y,rad,t:longint;
begin
rad:=x;
while tata[rad]<>rad do rad:=tata[rad];
multime:=rad;
y:=x;
while tata[y]<>y do
begin
t:=tata[y];
tata[y]:=rad;
y:=t;
end;
end;
procedure citire;
var cod:byte;
x,y,i,im,jm:longint;
begin
assign(input,'disjoint.in'); reset(input);
assign(output,'disjoint.out'); rewrite(output);
readln(n,m);
for i:=1 to n do
begin
tata[i]:=i;
h[i]:=1;
end;
for i:=1 to m do
begin
readln(cod,x,y);
im:=multime(x);
jm:=multime(y);
if cod=1 then
begin
if im<>jm then reuniune(im,jm)
end
else if im<>jm then writeln('NU')
else writeln('DA');
end;
close(input);
close(output);
end;
begin
citire;
end.