Cod sursa(job #459705)
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 50111
/*
*
*/
using namespace std;
bool isS[Nmax];
vector< int > r;
vector< int > G[Nmax];
inline void DFS( int x )
{
isS[x]=true;
if( G[x].empty() )
{
r.push_back(x);
return;
}
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
if( false == isS[*it] )
DFS(*it);
r.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( false == isS[x] )
DFS(x);
ofstream out( "sortaret.out" );
copy( r.rbegin(), r.rend(), ostream_iterator<int>( out, " " ) );
out<<'\n';
return EXIT_SUCCESS;
}