Pagini recente » Cod sursa (job #1217091) | Cod sursa (job #1082984) | Cod sursa (job #2270693) | Cod sursa (job #2672725) | Cod sursa (job #247161)
Cod sursa(job #247161)
{Arhiva educationala - multimi dijuncte}
var n,m,i,x,y,cod: longint;
t : array [1..100000] of longint;
f,g : text;
function tata (x:longint):longint;
var tx : longint;
begin
tx := x;
while (t[tx]>0) do tx := t[tx];
tata := tx;
end;
procedure join (x,y:longint);
var tx,ty : longint;
begin
tx := tata(x);
ty := tata(y);
if tx = ty then exit;
if t[tx] < t[ty] then
begin
t[tx] := t[tx] + t[ty];
t[ty] := tx;
end
else
begin
t[ty] := t[tx] + t[ty];
t[tx] := ty;
end;
end;
function query (x,y:longint):string;
var tx,ty : longint;
begin
tx := tata(x);
ty := tata(y);
if (tx = ty) then
query := 'DA'
else
query := 'NU';
end;
begin
assign (f,'disjoint.in');
assign (g,'disjoint.out');
reset (f);
rewrite (g);
readln (f,n,m);
for i:=1 to n do t[i] := -1;
for i:=1 to m do
begin
readln (f,cod,x,y);
if (cod = 1) then
join (x,y)
else
writeln(g, query (x,y));
end;
close (f);
close (g);
end.