Cod sursa(job #2172570)

Utilizator cubaLuceafarul cuba Data 15 martie 2018 16:59:05
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
const int nMax = 100005;
int St[nMax];
int k;
bool viz[nMax];
vector <int> G[nMax];
inline void Dfs(int node) {
    viz[node] = 1;
    for(const auto &v :  G[node]) {
        if(!viz[v]) {
            Dfs(v);
        }
    }
    St[++k] = node;
}
int main()
{
    int n, m, x, y;
    f >> n >> m;
    for(int i = 1; i <= m;i++) {
        f >> x >> y;
        G[x].push_back(y);
    }
    for(int i = 1; i <= n; i++) {
        if(!viz[i]) {
            Dfs(i);
        }
    }
    for(int i = k; i >= 1; i--) {
        g << St[i] << " ";
    }
    return 0;
}