Cod sursa(job #1362485)

Utilizator casianos1996Marc Casian Nicolae casianos1996 Data 26 februarie 2015 12:55:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1 kb
program dfs;
var     nr,x,y,i,j,k,n,m:longint;
        a:array of array of longint;
        viz,c:array of longint;
        bufin,bufout:array[1..200000] of byte;

procedure df(x:longint);
var       i:longint;
begin
  viz[x]:=1;
  for i:=1 to c[x] do
    if viz[a[x,i]]=0 then
    df(a[x,i]);
end;


begin
  assign(input,'dfs.in'); reset(input);
  assign(output,'dfs.out'); rewrite(output);
  settextbuf(input,bufin); settextbuf(output,bufout);
  readln(n,m);
  setlength(a,n+1,1);
  setlength(viz,n+1);
  setlength(c,n+1);
  for i:=1 to n do
    begin
      c[i]:=0;
      viz[i]:=0;
    end;
  for i:=1 to m do
    begin
      readln(x,y);
      c[x]:=c[x]+1;
      setlength(a[x],c[x]+1);
      c[y]:=c[y]+1;
      setlength(a[y],c[y]+1);
      a[y,c[y]]:=x;
      a[x,c[x]]:=y;
    end;
  nr:=0;
  for i:=1 to n do
    begin
      if viz[i]=0 then
        begin
          inc(nr);
          df(i);
        end;
    end;
  writeln(nr);
  close(input); close(output);
end.