Cod sursa(job #543066)

Utilizator BitOneSAlexandru BitOne Data 27 februarie 2011 15:16:05
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 500011

using namespace std;
bool was[N_MAX];
vector< int > v;
vector< int > G[N_MAX];
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;
}