Pagini recente » Cod sursa (job #1590688) | Cod sursa (job #534885) | Cod sursa (job #2609439) | Cod sursa (job #1300770) | Cod sursa (job #307110)
Cod sursa(job #307110)
program disjoint;
const MAX_N = 100000;
var n, m : longint;
t : array[1..MAX_N] of longint;
i, op, a, b : longint;
procedure union(a, b : longint);
begin
if random(2) = 1 then t[a] := b
else t[b] := a;
end;
function find(a : longint) : longint;
begin
if t[a] <> a then t[a] := find(t[a]);
find := t[a];
end;
// main function starts here
begin
assign(input, 'disjoint.in'); reset(input);
assign(output, 'disjoint.out'); rewrite(output);
read(n, m);
// initialization
for i := 1 to n do
t[i] := i;
// solve
for i := 1 to m do
begin
read(op, a, b);
if op = 1 then union(find(a), find(b))
else
if find(a) = find(b) then
writeln('DA')
else
writeln('NU');
end;
end.