Cod sursa(job #1637260)

Utilizator vladrochianVlad Rochian vladrochian Data 7 martie 2016 16:05:47
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <stack>
#include <vector>

using namespace std;

const int N_MAX = 5e4;

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

int N, M;
vector<int> G[N_MAX + 5];

bool use[N_MAX + 5];
stack<int> st;

void Dfs(int node) {
   use[node] = true;

   for (int son : G[node])
      if (!use[son])
         Dfs(son);

   st.push(node);
}

int main() {
   fin >> N >> M;
   while (M--) {
      int x, y;
      fin >> x >> y;
      G[x].push_back(y);
   }

   for (int i = 1; i <= N; ++i)
      if (!use[i])
         Dfs(i);

   while (!st.empty()) {
      fout << st.top() << " ";
      st.pop();
   }
   fout << "\n";
   return 0;
}