Pagini recente » Cod sursa (job #2751973) | Cod sursa (job #1992916) | Cod sursa (job #2677292) | Cod sursa (job #2466256) | Cod sursa (job #408785)
Cod sursa(job #408785)
{$M 64000000,0}
{$Inline on}
{$H-,I+,Q+,R+,S+}
{La Hoang
Ngay 3-3-2010}
const
TFI = 'disjoint.in';
TFO = 'disjoint.out';
MaxN = 100000;
var
fi, fo: text;
n, m, u, v, x, r1, r2: longint;
Lab: array[1..MaxN] of longint;
(*-----------------------------------*)
function Getroot(u: longint): longint; inline;
begin
while Lab[u] > 0 do u := Lab[u];
exit(u);
end;
(*-----------------------------------*)
procedure Union(r1, r2: longint); inline;
var
x: longint;
begin
x := Lab[r1] + Lab[r2];
if Lab[r1] > Lab[r2] then
begin
Lab[r1] := r2;
Lab[r2] := x;
end else
begin
Lab[r1] := x;
Lab[r2] := r1;
end;
end;
(*-----------------------------------*)
begin
Assign(fi, TFI); Reset(fi);
Assign(fo, TFO); Rewrite(fo);
Fillchar(Lab, sizeof(Lab), 255);
Readln(fi, n, m);
While m > 0 do
begin
dec(m);
Readln(fi, x, u, v);
r1 := Getroot(u);
r2 := Getroot(v);
if x = 1 then
if r1 <> r2 then Union(r1, r2);
if x = 2 then
if r1 = r2 then Writeln(fo, 'DA') else Writeln(fo, 'NU');
end;
Close(fo);
CLose(Fi);
end.