Cod sursa(job #649383)

Utilizator floriandreeaandreea floriandreea Data 15 decembrie 2011 22:22:50
Problema Parcurgere DFS - componente conexe Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 0.76 kb
#include<stdio.h>

typedef struct Node{int info; struct Node *next;}Node;
Node *G[10001];

int viz[10001];
int n,m;

void citire()
{
FILE *inf;
inf=fopen("dfs.in","r");
int i,z,y;
fscanf(inf,"%d %d",&n,&m);
for(i=1;i<=m;i++)
 {
  fscanf(inf,"%d %d",&z,&y);
  Node *p;
  if(!(p=(Node*)calloc(1,sizeof(Node)))) return ;
  p->info=y;
  p->next=G[z];
  G[z]=p;
 }
 fclose(inf);
}

void Dfs(int x)
{
Node *p;
viz[x]=1;
for(p=G[x];p;p=p->next)
      if(!viz[p->info])
      {
      viz[p->info]=1;
      Dfs(p->info);
      }     
}

void Afiseaza()
{
     
}


int main()
{
int i,nrcomp;
citire();
nrcomp=0;

for(i=1;i<=n;i++)
    if(viz[i]==0) { nrcomp++; Dfs(i); }
printf("%d \n",nrcomp);

system("pause");    
return 0;    
}