Cod sursa(job #522167)

Utilizator BitOneSAlexandru BitOne Data 14 ianuarie 2011 14:29:14
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
/* 
 * File:   main.cpp
 * Author: salexandru
 *
 * Created on January 14, 2011, 2:24 PM
 */
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 50011

using namespace std;

/*
 * 
 */
bool was[N_MAX];
vector< int > G[N_MAX];
vector< int > d;
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);
    d.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( d.rbegin(), d.rend(), ostream_iterator<int>(out, " " ) );
    out<<'\n';
    return 0;
}