Cod sursa(job #419939)

Utilizator RoCkyRomila RoCky Data 18 martie 2010 10:56:27
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#define NMAX 50002
std::ifstream in("sortaret.in");
std::ofstream out("sortaret.out");
int S[NMAX],n,m,v;
bool marc[NMAX];
struct lista{
	int nod;
	lista *next;
}*L[NMAX];
void adauga(int x,int y)
{
	lista *p;
	p = new lista;
	p->nod = y;
	p->next = L[x];
	L[x] = p;
}
void read()
{
	int i,x,y;
	in>>n>>m;
	for(i=0;i<m;i++)
	{
		in>>x>>y;
		adauga(x,y);
	}
}
void df(int nod)
{
	lista *p;
	marc[nod]=1;
	for(p=L[nod];p!=NULL;p=p->next)
		if(marc[p->nod]==0)
			df(p->nod);
	S[v++]=nod;
}
int main ()
{
	int i;
	read();
	for(i=1;i<=n;i++)
		df(i);
	for(i=n-1;i>=0;i--)
		out<<S[i]<<" ";
}