Cod sursa(job #186455)

Utilizator backqweryTester Pawl backqwery Data 27 aprilie 2008 23:09:41
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <stdio.h>

struct nod
{
	long inf;
	nod *next;
};
nod *l[50010], *in;
long s[50010], n, m;

void add(long x, long y)
{
	nod *p=new nod;
	p->inf=y;
	p->next=l[x];
	l[x]=p;
}
void df(long );
int main()
{
	freopen("sortaret.in", "r", stdin);
	freopen("sortaret.out", "w", stdout);

	long x, y;

	scanf("%ld %ld", &n, &m);

	for(long i=1; i<=m; i++)
	{
		scanf("%ld %ld", &x, &y);
		add(x, y);
	}
	for(i=1; i<=n; i++)
	{
		if(s[i]==0)
			df(i);
	}
	nod *p=in;
	while(p)
	{
		printf("%d ", p->inf);
		p=p->next;
	}

	return 0;
}
void df(long x)
{
	s[x]=1;
	nod *p=l[x];
	while(p)
	{
		if(s[p->inf]==0)
			df(p->inf);
		p=p->next;
	}

	nod *f=new nod;
	f->inf=x;
	f->next=in;
	in=f;
}