Cod sursa(job #408753)

Utilizator saodem74hieu tran saodem74 Data 3 martie 2010 10:50:06
Problema Paduri de multimi disjuncte Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 0.96 kb
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.