Cod sursa(job #478371)

Utilizator BitOneSAlexandru BitOne Data 18 august 2010 11:33:25
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
/* 
 * File:   main.cpp
 * Author: bitone
 *
 * Created on August 18, 2010, 11:26 AM
 */
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#define MAX_N 50011

using namespace std;

/*
 * 
 */
typedef signed int sint;
bool check[MAX_N];
vector< sint > DfsOrder;
vector< sint > G[MAX_N];
inline void DFS( sint x )
{
    check[x]=true;
    vector< sint >::const_iterator it=G[x].begin(), iend=G[x].end();
    for( ; it < iend; ++it )
        if( !check[*it] )
            DFS( *it );
    DfsOrder.push_back(x);
}
int main( void )
{
    sint 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( !check[x] )
            DFS(x);
    ofstream out( "sortaret.out" );
    copy( DfsOrder.rbegin(), DfsOrder.rend(), ostream_iterator<sint>(out, " ") );
    out<<'\n';
    return EXIT_SUCCESS;
}