Pagini recente » Cod sursa (job #3206841) | Cod sursa (job #1693106) | Cod sursa (job #1235783) | Cod sursa (job #2046063) | Cod sursa (job #408753)
Cod sursa(job #408753)
const tfi='disjoint.in';
tfo='disjoint.out';
maxn=100100;
var fi,fo:text;
m,n:longint;
lab:array[0..maxn] of longint;
function getroof(u:longint):longint;
begin
while lab[u]>0 do u:=lab[u];
exit(u);
end;
procedure union(u,v:Longint);
var x:longint;
begin
x:=lab[u]+lab[v];
if lab[u]>lab[v] then
begin
lab[u]:=v;
lab[v]:=x;
end
else
begin
lab[v]:=u;
lab[u]:=x;
end;
end;
procedure process;
var ii,jj,i,j,u,v:longint;
begin
fillchar(lab,sizeof(lab),255);
read(fi,n,m);
for i:=1 to m do
begin
read(fi,j,u,v);
if j=1 then
begin
ii:=getroof(u);
jj:=getroof(v);
if ii<>jj then union(ii,jj);
end
else
begin
if getroof(u)=getroof(v) then writeln(fo,'DA') else writeln(fo,'NU');
end;
end;
end;
begin
assign(fi,tfi); reset(fi);
assign(Fo,tfo); rewrite(fo);
process;
close(fi); close(fo);
end.