Pagini recente » Cod sursa (job #2580564) | Cod sursa (job #2353800) | Cod sursa (job #1387330) | Cod sursa (job #453073) | Cod sursa (job #293674)
Cod sursa(job #293674)
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.