Pagini recente » Cod sursa (job #2868261) | if.mate-info.tk | Istoria paginii runda/iv/clasament | Statistici Shizue Jimenez (lacreshaee2196) | Cod sursa (job #885721)
Cod sursa(job #885721)
program multimi;
var f,g:text;
x,y,z,m,n,i,x1,x2:longint;
t,r:array[1..100001] of longint;
bufin,bufout:array[1..65000] of byte;
function arb(x:longint):longint;
begin
if x=t[x] then
arb:=x else
begin
t[x]:=arb(t[x]);
arb:=t[x];
end;
end;
procedure uneste(x,y:longint);
begin
if (r[x]>r[y]) then begin
t[y]:=arb(x);
end
else
begin
t[x]:=arb(y);
end;
if r[x]=r[y] then r[y]:=r[y]+1;
end;
begin
assign(f,'disjoint.in'); reset(f);
assign(g,'disjoint.out'); rewrite(g);
settextbuf(f,bufin);
settextbuf(g,bufout);
readln(f,n,m);
for i:=1 to n do
begin
t[i]:=i;
r[i]:=1;
end;
for i:=1 to m do
begin
readln(f,x,y,z);
if x=1 then begin
uneste(y,z);
end else if t[y]=t[z] then
writeln(g,'DA') else
writeln(g,'NU');
end;
close(f); close(g);
end.