Pagini recente » Cod sursa (job #2028395) | Cod sursa (job #880387) | Cod sursa (job #1057847) | test_penali | Cod sursa (job #305170)
Cod sursa(job #305170)
program disjoint;
type nod = ^articol;
articol = record
root:nod;
l:integer;
end;
var i,p,q,n,m:longint;
c:byte;
a:array [1..100000] of nod;
procedure union(var m1,m2:nod);
begin
while m1^.root <> m1 do
m1:=m1^.root;
while m2^.root <> m2 do
m2:=m2^.root;
m1^.root:=m2;
m2^.l:=m2^.l+m1^.l;
end;
function find(nd:nod):nod;
begin
if nd^.root = nd then
find:=nd
else
begin
nd^.root:=find(nd^.root);
find:=nd^.root;
end;
end;
begin
assign(input,'disjoint.in');
assign(output,'disjoint.out');
reset(input);
rewrite(output);
readln(n,m);
for i:=1 to n do
begin
new(a[i]);
a[i]^.root:=a[i];
a[i]^.l:=1;
end;
for i:=1 to m do
begin
readln(c,p,q);
if c = 1 then
begin
if a[p]^.l<a[q]^.l then
union(a[p],a[q])
else
union(a[q],a[p]);
end
else
if find(a[q]) = find(a[p]) then
writeln('DA')
else
writeln('NU');
end;
close(input);
close(output);
end.