Cod sursa(job #459709)
#include <queue>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 50111
/*
*
*/
using namespace std;
int d[Nmax];
vector< int > r;
vector< int > G[Nmax];
vector< int >::const_iterator it, iend;
int main( void )
{
unsigned int N, M, x, y;
ifstream in( "sortaret.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
G[x].push_back(y);
++d[y];
}
for( x=1; x <= N; ++x )
if( 0 == d[x] )
r.push_back(x);
for( x=0; x < N; ++x )
{
y=r[x];
for( it=G[y].begin(), iend=G[y].end(); it < iend; ++it )
{
--d[*it];
if( 0 == d[*it] )
{
r.push_back(*it);
if( N == r.size() )
{
ofstream out( "sortaret.out" );
copy( r.begin(), r.end(), ostream_iterator<int>( out, " " ) );
out<<'\n';
return EXIT_SUCCESS;
}
}
}
}
return EXIT_SUCCESS;
}