Cod sursa(job #350684)

Utilizator radu_cppRadu Voroneanu radu_cpp Data 25 septembrie 2009 14:18:43
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <stdio.h>

struct varf{
	int val;
	varf *urm;
};

varf *a[50001];
varf *aux;
int c[50001], grad[50001];
int i,x,y,n,m;

int main()
{
	freopen("sortaret.in","r",stdin);
	freopen("sortaret.out","w",stdout);
	scanf("%d %d",&n,&m);
	for (i=1; i<=n; a[i]=NULL, i++);
	for (i=1; i<=m; i++){
		scanf("%d %d",&x,&y);
		aux= new varf;
		aux->urm=a[x];
		aux->val=y;
		a[x]=aux;
		grad[y]++;
	}
	for (i=1; i<=n; i++)
		if (grad[i]==0)
			c[++c[0]]=i;
	for (i=1; i<=n; i++){
		x=c[i];
		for (aux=a[x]; aux!=NULL; aux=aux->urm){
			grad[aux->val]--;
			if (grad[aux->val]==0) c[++c[0]]=aux->val;
		}
	}
	for (i=1; i<=n; i++)
		printf("%d ",c[i]);
	printf("\n");
	return 0;
}