Cod sursa(job #446012)

Utilizator alexandru92alexandru alexandru92 Data 24 aprilie 2010 19:16:25
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on April 24, 2010, 7:06 PM
 */
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 50001

/*
 * 
 */
using namespace std;
bool uz[Nmax];
vector< int > dfn;
vector< int > G[Nmax];
inline void DFS( int x )
{
    uz[x]=true;
    vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
    for( ; it < iend; ++it )
        if( !uz[*it] )
            DFS( *it );
    dfn.push_back( x );
}
int main(int argc, char** argv)
{
    int N, M, x, y;
    ifstream in( "sortaret.in" );
    in>>N>>M;
    for( ; M; --M )
    {
        in>>x>>y;
        G[x].push_back(y);
    }
    for( x=1; x <= N; ++x )
        if( !uz[x] )
            DFS(x);
    ofstream out( "sortaret.out" );
    copy( dfn.rbegin(), dfn.rend(), ostream_iterator<int>( out, " " ) );
    out<<'\n';
    return (EXIT_SUCCESS);
}