Cod sursa(job #2210888)

Utilizator dropsdrop source drops Data 8 iunie 2018 15:25:47
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <unordered_map>
#include <set>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
using namespace std;

vector<int> e[50050];
bool marked[50050];
vector<int> sol;

void Dfs(int x) {
  marked[x] = true;
  for (auto y : e[x]) {
    if (!marked[y]) Dfs(y);
  }
  sol.push_back(x);
}

int main() {
  freopen("sortaret.in","r",stdin);
  freopen("sortaret.out","w",stdout);
  int n, m, x, y;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %d", &x, &y);
    e[x].push_back(y);
  }
  for (int i = 1; i <= n; ++i) {
    if (!marked[i]) {
	Dfs(i);
    }
  }
  reverse(sol.begin(), sol.end());
  for (auto x : sol) {
    printf("%d ", x);
  }

  return 0;
}