Cod sursa(job #504806)

Utilizator BitOneSAlexandru BitOne Data 28 noiembrie 2010 19:19:19
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
#define MAX_N 50011

using namespace std;

/*
 *
 */
bool was[MAX_N];
vector< int > v;
vector< int > G[MAX_N];
inline void DFS( int x )
{
	was[x]=true;
	vector<int>::const_iterator it=G[x].begin(), iend=G[x].end();
	for( ; it < iend; ++it )
		if( !was[*it] )
			DFS(*it);
	v.push_back(x);
}
int main( void )
{
	int N, M, x, y;
	ifstream in( "sortaret.in" );
	for( in>>N>>M; M; --M )
	{
		in>>x>>y;
		G[x].push_back(y);
	}
	for( x=1; x <= N; ++x )
		if( !was[x] )
			DFS(x);
	ofstream out( "sortaret.out" );
	copy( v.rbegin(), v.rend(), ostream_iterator<int>( out, " " ) );
	out<<'\n';
	return EXIT_SUCCESS;
}