Cod sursa(job #1191043)

Utilizator BitOneSAlexandru BitOne Data 26 mai 2014 13:08:22
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <array>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>

using namespace std;
const int NMAX = 50011;

vector<int> v;
array<bool, NMAX> was;
array<vector<int>, NMAX> G;

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

    for(const auto& y: G[x])
    {
	if(!was[y]) DFS(y);
    }

    v.push_back(x);
}

int main()
{
    int N, M, x, y;
    ifstream in{"sortaret.in"};
    ofstream out{"sortaret.out"};

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

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

    return EXIT_SUCCESS;
}