Cod sursa(job #459706)

Utilizator BitOneSAlexandru BitOne Data 30 mai 2010 19:52:49
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#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.in" );
                     copy( r.begin(), r.end(), ostream_iterator<int>( out, " " ) );
                     out<<'\n';
                     return EXIT_SUCCESS;
                 }
             }
         }
     }
     return EXIT_SUCCESS;
 }