Pagini recente » Cod sursa (job #1533004) | Cod sursa (job #48444) | Cod sursa (job #220741) | Cod sursa (job #1341250) | Cod sursa (job #1073760)
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;
var t,aux:longint;
begin
aux:=x;
while tata[x]<>x do
x:=tata[x];
while tata[aux]<>aux do begin
t:=tata[aux];
tata[aux]:=x;
aux:=t;
end;
radacina:=x;
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.