Cod sursa(job #459558)

Utilizator alexandru92alexandru alexandru92 Data 30 mai 2010 10:11:00
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 50111

/*
 *
 */
 using namespace std;
 bool wash[Nmax];
 vector< int > dfs;
 vector< int > G[Nmax];
 inline void DFS( int x )
 {
     wash[x]=true;
     if( G[x].empty() )
     {
         dfs.push_back(x);
         return;
     }
     vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
     for( ; it < iend; ++it )
        if( false == wash[*it] )
            DFS(*it);
    dfs.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 == wash[x] )
            DFS(x);
    ofstream out( "sortaret.out" );
    copy( dfs.rbegin(), dfs.rend(), ostream_iterator<int>( out, " " ) );
    out<<'\n';
    return EXIT_SUCCESS;
 }