Cod sursa(job #1606739)

Utilizator noi_totinoi toti noi_toti Data 20 februarie 2016 14:51:40
Problema Parcurgere DFS - componente conexe Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 1.18 kb
program conex;
var f,g:text;
a:array[1..10000,1..10000] of longint;
n,m,c,p,i,j:longint;
viz:array[1..100001] of integer;
procedure citire;
var i,j,l:integer;
begin
     assign(f,'dfs.in');
     reset(f);
     assign(g,'dfs.out');
     rewrite(g);
     readln(f,n,m);
     for l:=1 to m do
           begin
              readln(f,i,j);
              a[i,j]:=1;
              a[j,i]:=1;
           end;
end;
procedure df(x:integer);
var i:integer;
begin
      if viz[x]=0 then
           begin
               viz[x]:=1;
               for i:=1 to n do
                  if (a[x,i]=1) and (viz[i]=0) then
                    begin
                     c:=c+1;
                     df(i);
                    end;
           end;
end;
begin
    citire;
    p:=0;
    for i:=1 to n do
       begin
         c:=0;
         df(i);
         if c<>0 then
             p:=p+1;
       end;
    for i:=1 to n do
       begin
         c:=0;
         for j:=1 to n do
            begin
              if a[i,j]=0 then
                  c:=c+1;
            end;
         if c=n then
             p:=p+1;
       end;
    write(g,p);
    close(f);
    close(g);
end.