Cod sursa(job #2503112)

Utilizator ValentinStStamate Valentin ValentinSt Data 2 decembrie 2019 15:07:27
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;

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

int n, m;
stack<int> s;
vector<int> G[50001];
bool viz[50001];

void DFS(int i);

int main(){
  
  in>>n>>m;

  for(int i = 1; i <= m; i++){
    int x, y;
    in>>x>>y;
    G[x].push_back(y);
  }
  
  for(int i = 1; i <= n; i++){
    if(viz[i] == false){
      DFS(i);
    }
  }

  while(!s.empty()){
    out<<s.top()<<" ";
    s.pop();
  }

  return 0;
}

void DFS(int i){
  viz[i] = true;

  for(int l = 0; l < G[i].size(); l++){
    int j = G[i].at(l);
    
    if(viz[j] == false)
      DFS(j);
  }

  s.push(i);

}