Pagini recente » Clasament dupa rating | Cod sursa (job #2301664) | Cod sursa (job #1783932) | Cod sursa (job #501438) | Cod sursa (job #701438)
Cod sursa(job #701438)
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 50011
using namespace std;
bool was[N_MAX];
vector< int > topSort;
vector< int > G[N_MAX];
inline void DFS( int x )
{
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
was[x]=true;
for( ; it < iend; ++it )
if( false == was[*it] )
DFS(*it);
topSort.push_back(x);
}
int main()
{
int N, M, x, y;
ifstream in( "sortaret.in" );
ofstream out( "sortaret.out" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
G[x].push_back(y);
}
for( x=1; x <= N; ++x )
if( false == was[x] )
DFS(x);
copy( topSort.rbegin(), topSort.rend(), ostream_iterator<int>( out, " " ) );
out<<'\n';
return EXIT_SUCCESS;
}