Cod sursa(job #408764)

Utilizator saodem74hieu tran saodem74 Data 3 martie 2010 10:55:17
Problema Paduri de multimi disjuncte Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 0.97 kb
{$Inline on}
const tfi='disjoint.in';
      tfo='disjoint.out';
      maxn=100002;
var   fi,fo:text;
      m,n:longint;
      lab:array[0..maxn] of longint;

function getroof(u:longint):longint;  inline;
var v:longint;
begin
//  v:=u;
  while lab[u]>0 do u:=lab[u];
 // lab[v]:=u;
  exit(u);
end;

procedure union(u,v:Longint); inline;
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);
    ii:=getroof(u);
    jj:=getroof(v);
    if j=1 then
       if ii<>jj then union(ii,jj) else
    else
      if ii=jj then writeln(fo,'DA') else writeln(fo,'NU');
   end;
end;

begin
  assign(fi,tfi); reset(fi);
  assign(Fo,tfo); rewrite(fo);
  process;
  close(fi); close(fo);
end.