Cod sursa(job #2256050)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 7 octombrie 2018 20:58:57
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

const int N = 50000;

int n, m;
vector <int> v[N];
bool seen[N];
vector <int> stk;


inline void DFS(int nod) {
  seen[nod] = 1;
  for (auto &to: v[nod]) {
    if (seen[to] == 0) {
      DFS(to);
    }
  }
  stk.push_back(nod);
}

int main() {
  fin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    fin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (seen[i] == 0) {
      DFS(i);
    }
  }
  reverse(stk.begin(), stk.end());
  for (auto &x: stk) {
    x++;
    fout << x << " ";
  }
  fout << "\n";
  return 0;
}