Pagini recente » Cod sursa (job #857363) | Cod sursa (job #708231) | Cod sursa (job #570271) | Cod sursa (job #2863413) | Cod sursa (job #408762)
Cod sursa(job #408762)
{DINH QUANG DAT TIN 07-10}
{DISJOINT}
{$inline on}
CONST
TFI='disjoint.in';
TFO='disjoint.out';
MAX=1000001;
TYPE
arr1int=array[0..MAX] of longint;
VAR
fi,fo:text;
i,r1,r2,m,u,v,task,p,n:longint;
lab:arr1int;
PROCEDURE union(r1,r2:longint);inline;
begin
if lab[r1]<lab[r2] then
begin
lab[r1]:=lab[r1]+lab[r2];
lab[r2]:=r1;
end
else
begin
lab[r2]:=lab[r1]+lab[r2];
lab[r1]:=r2;
end;
end;
FUNCTION getroot(v:longint):longint;inline;
var
x:longint;
begin
x:=v;
while lab[x]>0 do x:=lab[x];
getroot:=x;
x:=v;
while lab[x]>0 do
begin
v:=lab[x];
lab[x]:=getroot;
x:=v;
end;
end;
BEGIN
assign(fi,tfi);reset(fi);
assign(fo,tfo);rewrite(fo);
read(fi,n,m);
for i:= 1 to n do lab[i]:=-1;
for i:= 1 to m do
begin
read(fi,task,u,v);
r1:=getroot(u);
r2:=getroot(v);
if task=1 then
begin
if r1<>r2 then union(r1,r2);
end
else if r1<>r2 then writeln(fo,'NU') else writeln(fo,'DA');
end;
close(fo);
close(fi);
END.