Pagini recente » Cod sursa (job #3201995) | Cod sursa (job #1745402) | Cod sursa (job #340327) | Cod sursa (job #1911306) | Cod sursa (job #305183)
Cod sursa(job #305183)
program disjoint;
var root,sz:array [1..100000] of longint;
aux1,aux2,n,m,p,q,c,i:longint;
function find(nod:longint):longint;
begin
if nod = root[nod] then
find:=nod
else
begin
root[nod]:=find(root[nod]);
find:=root[nod];
end;
end;
procedure union(m1,m2:longint);
begin
aux1:=find(m1);
aux2:=find(m2);
root[aux1]:=aux2;
if sz[aux1] = sz[aux2] then
inc(sz[aux2]);
end;
begin
assign(input,'disjoint.in');
assign(output,'disjoint.out');
reset(input);
rewrite(output);
readln(n,m);
for i:=1 to n do
begin
root[i]:=i;
sz[i]:=1;
end;
for i:=1 to m do
begin
readln(c,p,q);
if c = 1 then
begin
if sz[p]<sz[q] then
union(p,q)
else
union(q,p);
end
else
if find(p) = find(q) then
writeln('DA')
else
writeln('NU');
end;
close(input);
close(output);
end.