Pagini recente » Cod sursa (job #1800431) | Cod sursa (job #256838) | Cod sursa (job #2043418) | Cod sursa (job #2648948) | Cod sursa (job #1172504)
program Paduri_de_multimi_disjuncte;
var parent:array[0..100000] of longint;
n,m,i,j,x,y,op,a,b,g,f:longint;
function find(x:longint):longint;
begin
if x=parent[x] then
find:=x
else
begin
find:=
find(parent[x]);
end;
end;
begin
assign(input,'disjoint.in');
reset(input);
assign(output,'disjoint.out');
rewrite(output);
readln(n,m);
for i:=1to n do parent[i]:=i;
for j:=1 to m do
begin
readln(op,x,y);
if op=1 then begin
g:=find(x);
while parent[x]<>g do
begin
f:=parent[x];
x:=parent[f];
parent[f]:=g;
end;
A:=find(y);
while parent[y]<>a do
begin
b:=parent[y];
y:=parent[b];
parent[b]:=a;
end;
parent[g]:=a;
end
else
begin
if
find(x)=find(y) then
writeln('DA') else
writeln('NU');
end;
end;
readln;
end.