Cod sursa(job #492083)

Utilizator BitOneSAlexandru BitOne Data 13 octombrie 2010 13:27:28
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
/* 
 * File:   main.cpp
 * Author: bitone
 *
 * Created on October 13, 2010, 1:20 PM
 */
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define MAX_N 50011

using namespace std;

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