Pagini recente » Cod sursa (job #2550637) | Cod sursa (job #1110387) | Cod sursa (job #735358) | Cod sursa (job #1391487) | Cod sursa (job #934432)
Cod sursa(job #934432)
program paduri;
var f,g:text;
t:array[1..100000] of longint;
rg:array[1..100000] of longint;
n,m,x,y,key,i:longint;
bufin,bufout:array[1..65000] of byte;
function tata (x:longint):longint;
begin
while x<>t[x] do x:=t[x];
tata:=x;
end;
procedure uneste (x,y:longint);
begin
if rg[x]<rg[y] then
t[x]:=y
else t[y]:=x;
if rg[x]=rg[y] then inc(rg[y]);
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; rg[i]:=1;
end;
for i:=1 to m do
begin
readln (f,key,x,y);
if key=1 then
begin
uneste(tata(x), tata(y));
end
else
begin
if tata(x)=tata(y) then
writeln (g,'DA')
else writeln (g,'NU');
end;
end;
close (F); close (G);
end.