Pagini recente » Cod sursa (job #1838529) | Cod sursa (job #703222) | Cod sursa (job #2947026) | Cod sursa (job #3212427) | Cod sursa (job #408754)
Cod sursa(job #408754)
{FP}
{$M 64000000,0}
{$MODE OBJFPC}
{$IFDEF ANHDQ}
{$INLINE OFF}
{$H-,I+,Q+,R+,S+}
{$ELSE}
{$INLINE ON}
{$H+,I-,Q-,R-,S-}
{$ENDIF}
// Result:
program disjoint_AnhDQ;
const
FI_NAME = 'disjoint.in';
FO_NAME = 'disjoint.out';
MaxN = 100000;
var
n, m, t, v1, v2, r1, r2: LongInt;
Fa, nC : array[1..MaxN] of LongInt;
(*------------------------------*)
function GetRoot(v: LongInt): LongInt; inline;
begin
if Fa[v] = 0 then exit(v)
else Fa[v] := GetRoot(Fa[v]);
exit(Fa[v]);
end;
(*------------------------------*)
procedure Union(r1, r2: LongInt); inline;
begin
if nC[r1] > nC[r2] then
begin
Fa[r2] := r1;
Inc(nC[r1], nC[r2]);
end
else
begin
Fa[r1] := r2;
Inc(nC[r2], nC[r1]);
end;
end;
(*------------------------------*)
begin
Assign(Input, FI_NAME); Reset(Input);
Assign(Output, FO_NAME); Rewrite(Output);
read(n, m);
for t := 1 to n do nC[t] := 1;
while m > 0 do
begin
Dec(m);
read(t, v1, v2);
r1 := GetRoot(v1);
r2 := GetRoot(v2);
if t = 1 then
if r1 <> r2 then Union(r1, r2)
else
else if r1 = r2 then WriteLn('DA')
else WriteLn('NU');
end;
end.