Cod sursa(job #649382)

Utilizator floriandreeaandreea floriandreea Data 15 decembrie 2011 22:20:55
Problema Componente tare conexe Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 0.89 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,int contor)
{
Node *p;
viz[x]=contor;
for(p=G[x];p;p=p->next)
      if(!viz[p->info])
      {
      viz[p->info]=contor;
      Dfs(p->info,contor);
      }     
}

void Afiseaza()
{
     
}


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

for(i=1;i<=n;i++)
    if(viz[i]==0) { Dfs(i,++contor); }


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

system("pause");    
return 0;    
}