Cod sursa(job #947000)

Utilizator BitOneSAlexandru BitOne Data 6 mai 2013 15:14:03
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>

using namespace std;

const int NMAX = 50011;


bool was[NMAX];
vector<int> dfs;
vector<int> G[NMAX];

inline void DFS(int x)
{
    was[x] = true;

    for(auto& y : G[x])
    {
        if(!was[y])
        {
            DFS(y);
        }
    }
    dfs.push_back(x);
}
int main()
{
    int N, M, x, y;
    ifstream in("sortaret.in");
    ofstream out("sortaret.out");

    for(in >> N >> M; M; --M)
    {
        in >> x >> y;
        G[x].push_back(y);
    }
    dfs.reserve(N);
    for(x = 1; x <= N; ++x)
        if(!was[x])
            DFS(x);

    copy(dfs.rbegin(), dfs.rend(), ostream_iterator<int>(out, " "));
    out << '\n';

    return EXIT_SUCCESS;
}