Pagini recente » Cod sursa (job #419027) | Cod sursa (job #2356879) | Cod sursa (job #1454222) | Cod sursa (job #2666005) | Cod sursa (job #1623842)
program disj;
var f, g:text;
v:array of longint;
bufin, bufout:array[1..1 shl 16] of byte;
n, m, i, cod, x, y:longint;
function find(nod:longint):longint;
begin
while v[nod] > 0 do nod := v[nod];
find := nod;
end;
function find2(nod:longint):longint;
var aux:longint;
begin
aux := nod;
while v[nod] > 0 do nod := v[nod];
find2 := nod;
while v[aux] > 0 do
begin
v[aux] := find2;
aux := v[aux];
end;
end;
begin
assign(f, 'disjoint.in'); reset(f);
assign(g, 'disjoint.out'); rewrite(g);
settextbuf(f, bufin); settextbuf(g, bufout);
readln(f, n, m); setlength(v, n + 2);
for i := 1 to m do
begin
readln(f, cod, x, y);
if cod = 1 then
v[find(y)] := find(x)
else
begin
if find2(x) = find2(y) then writeln(g, 'DA')
else writeln(g, 'NU');
end;
end;
close(f); close(g);
end.