Cod sursa(job #846664)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 2 ianuarie 2013 16:38:12
Problema Parcurgere DFS - componente conexe Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.83 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;
      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;
  for i:=1 to n do
    if viz[i]=0 then begin inc(k); dfs(i); end;
  writeln(f2,k);
  close(f1);close(f2);
end.