Cod sursa(job #1238049)

Utilizator alexandru92alexandru alexandru92 Data 5 octombrie 2014 15:21:17
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <array>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>

using namespace std;
const int NMAX = 50011;
using Graph = array<vector<int>, NMAX>;

Graph G;
vector<int> stack;
array<bool, NMAX> was;

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

  for (const auto& y: G[x]) {
    if (was[y]) {
      DFS(y);
    }
  } 
  stack.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);
  }

  for (int i = 1; i <= N; ++i) {
    if (!was[i]) {
      DFS(i);
    }
  }

  copy(stack.begin(), stack.end(), ostream_iterator<int>{out, " "});

  return EXIT_SUCCESS;
}