Cod sursa(job #334676)

Utilizator alex.cepoiAlexandru Cepoi alex.cepoi Data 27 iulie 2009 16:44:09
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;

#define nmax 50001
#define mmax 100001

int N,M;
vector <int> V[nmax];
bitset <nmax> viz;

int search ()
{
	static int i = 0;
	while (++i<=N)
		if (!viz[i]) return i;
	return 0;
}

void df (int i)
{
	for (int j=0; j<V[i].size(); ++j)
		if (!viz[V[i][j]]) df (V[i][j]);

	viz[i]=1;
	cout<<i<<' ';
}

int main ()
{
	freopen ("sortaret.in","r",stdin);
	freopen ("sortaret.out","w",stdout);

	cin>>N>>M;
	while (M--)
	{
		int a,b; cin>>a>>b;
		V[b].push_back(a);
	}
	
	int x=1;
	while (x)
	{
		df(x);
		x=search();
	}
	return 0;
}