Pagini recente » Clasament g | Cod sursa (job #1523964) | Cod sursa (job #2348948) | Cod sursa (job #1749680) | Cod sursa (job #343746)
Cod sursa(job #343746)
var t,gr:array[1..100000] of longint;
n,m,i,cod,x,y,s1,s2,aux:longint;
f,g:text;
function s(el:longint):longint;
var str:longint;
begin
str:=el;
while t[str]<>str do
str:=t[str];
if cod=2 then
while el<>str do
begin
aux:=t[el];
t[el]:=str;
el:=aux;
end;
s:=str;
end;
begin
assign(f,'disjoint.in');
assign(g,'disjoint.out');
reset(f);rewrite(g);
readln(f,n,m);
for i:=1 to n do
begin
t[i]:=i;
gr[i]:=1;
end;
for i:=1 to m do
begin
readln(f,cod,x,y);
if cod=1 then
begin
s1:=s(x);
s2:=s(y);
if gr[s1]>=gr[s2] then
begin
t[s2]:=s1;
if gr[s1]=gr[s2] then
inc(gr[s1]);
while x<>s1 do
begin
aux:=t[x];
t[x]:=s1;
x:=aux;
end;
while y<>s1 do
begin
aux:=t[y];
t[y]:=s1;
y:=aux;
end;
end
else
begin
t[s1]:=s2;
while x<>s2 do
begin
aux:=t[x];
t[x]:=s2;
x:=aux;
end;
while y<>s2 do
begin
aux:=t[y];
t[y]:=s2;
y:=aux;
end;
end;
end
else
if s(x)=s(y) then
writeln(g,'DA')
else
writeln(g,'NU');
end;
close(f);close(g);
end.