Pagini recente » Cod sursa (job #623546) | Cod sursa (job #1492745) | Cod sursa (job #1615213) | Cod sursa (job #2861233) | Cod sursa (job #892646)
Cod sursa(job #892646)
program info;
var t,h:array[1..100000] of longint;
n,m:longint;
f,g:text;
function tata(x:integer):integer;
BEGIN
if x=t[x] then
tata:=x
else
BEGIN
t[x]:=tata(t[x]);
tata:=t[x];
END;
END;
procedure citire;
var i,op,x,y:longint;
BEGIN
assign(f,'disjoint.in'); reset(f);
assign(g,'disjoint.out'); rewrite(g);
readln(f,n,m);
for i:=1 to n do
BEGIN
t[i]:=i;
h[i]:=1;
END;
for i:=1 to m do
BEGIN
readln(f,op,x,y);
if op=1 then
BEGIN
if tata(x)<>tata(y) then
if h[x]>h[y] then
t[y]:=tata(x)
else
if h[x]<h[y] then
t[x]:=tata(y)
else
BEGIN
t[x]:=tata(y);
h[y]:=h[y]+1;
END;
END
else
BEGIN
if tata(x)=tata(y) then
writeln(g,'DA')
else
writeln(g,'NU');
END;
END;
END;
BEGIN
citire;
close(f);
close(g);
END.