Cod sursa(job #371979)

Utilizator raizenIancu Andrei raizen Data 7 decembrie 2009 23:12:08
Problema Parcurgere DFS - componente conexe Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 0.58 kb
#include <stdio.h>
 int a[1005][1005], viz[1005],n,m,z;
void citire ()
 {
   freopen("dfs.in", "r", stdin);
   freopen("dfs.out", "w",stdout);
    scanf(" %d % d", & n, & m);
     int i,x,y;
     for(i=1;i<=m;i++)
       {
        scanf(" % d %d", & x, &y);
        a[x][y]=a[y][x]=1;
        }
  }
void DFS (int k)
{  
  int i;
  viz[k]=1;
  for(i=1;i<=n;i++)
    if(!viz[i] && a[k][i])
    DFS(i);
}
int main()
{
  citire();
  int i;
  for(i=1;i<=n;i++)
    if(!viz[i])
     {
       z++;
       DFS(i);
      }
   printf(" %d\n",  z);
return 0;
}