Pagini recente » Cod sursa (job #384996) | Cod sursa (job #1338310) | Cod sursa (job #2310149) | Cod sursa (job #439599) | Cod sursa (job #1075365)
program disjoint;
var tata,rg:array[1..100000] of longint;
f,g:text;
tip,n,m,i,x,y,q,w:longint;
function radacina(x:longint):longint;
begin
if tata[x]=x then
radacina:=x
else
begin
tata[x]:=radacina(tata[x]);
radacina:=tata[x];
end;
end;
begin
assign(f,'disjoint.in'); reset(f);
assign(g,'disjoint.out'); rewrite(g);
read(f,n);
for i:=1 to n do
begin
tata[i]:=i;
rg[i]:=1;
end;
read(f,m);
for i:=1 to m do
begin
read(f,tip,x,y);
q:=radacina(x);
w:=radacina(y);
case tip of
1: begin
if rg[w]>rg[q] then
tata[q]:=w
else
tata[w]:=q;
if rg[w]=rg[q] then
inc(rg[q]);
end;
2: begin
if q=w then
writeln(g,'DA')
else
writeln(g,'NU');
end;
end;
end;
close(f); close(g);
end.