Cod sursa(job #184482)

Utilizator katakunaCazacu Alexandru katakuna Data 23 aprilie 2008 18:29:42
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include<stdio.h>

int *v[50110];
int x,sol[50111],k,n,i,nr[50110],viz[50011],m,y,t[50110];


void parc(int x){
int i;
viz[x]=1;

  for(i=1;i<=v[x][0];i++){
    if(!viz[v[x][i]])parc(v[x][i]);
  }


k++;
sol[k]=x;

}



int main(){


FILE *f=fopen("sortaret.in","r");
fscanf(f,"%d %d",&n,&m);

  for(i=1;i<=m;i++){
  fscanf(f,"%d %d",&x,&y);
  nr[x]++;
  t[y]=1;
  }

fclose(f);

  for(i=1;i<=n;i++){
  v[i]=new int [nr[i]+3];
  }


  for(i=1;i<=n;i++)
  v[i][0]=0;


FILE *ff=fopen("sortaret.in","r");
fscanf(ff,"%d %d",&n,&m);

  for(i=1;i<=m;i++){
  fscanf(f,"%d %d",&x,&y);
  //nr[x]++;
  v[x][0]++;
  v[x][v[x][0]]=y;
  }

fclose(ff);

for(i=1;i<=n;i++){
if(!t[i])break;
}


parc(i);

FILE *g=fopen("sortaret.out","w");
for(i=k;i>=1;i--)
fprintf(g,"%d ",sol[i]);
fclose(g);
return 0;
}