Cod sursa(job #846640)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 2 ianuarie 2013 16:02:07
Problema Parcurgere DFS - componente conexe Scor 70
Compilator fpc Status done
Runda Arhiva educationala Marime 0.96 kb
program dfs;
  type vdynamic=array of longint;
  var f1,f2:text;
      n,m,i,x,y,k:longint;
      a:array [1..100000] of vdynamic;
      viz:array [1..100000] of byte;
      t:boolean;
      buf:array[1..100000] of byte;
procedure dfs(x:longint);
  var j:longint;
  begin
    viz[x]:=1;
    for j:=1 to length(a[x])-1 do
      if viz[a[x,j]]=0 then dfs(a[x,j]);
  end;
begin
  assign(f1,'dfs.in');
  reset(f1);
  settextbuf(f1,buf);
  assign(f2,'dfs.out');
  rewrite(f2);
  readln(f1,n,m);
  for i:= 1 to n do setlength(a[i],1);
  for i:=1 to m do
    begin
      readln(f1,x,y);
      setlength(a[x],length(a[x])+1);
      a[x,length(a[x])-1]:=y;
      setlength(a[y],length(a[y])+1);
      a[y,length(a[y])-1]:=x;
    end;
  t:=true;
  while t do
    begin
      t:=false;
      i:=1;
      while (viz[i]<>0)and(i<=n) do inc(i);
      if i<>n+1 then begin inc(k);dfs(i);t:=true;end;
    end;
  writeln(f2,k);
  close(f1);close(f2);
end.