Cod sursa(job #1912567)

Utilizator adiXMGemene Adrian adiXM Data 8 martie 2017 09:39:15
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 = 50003;
vector <int> Graf[nMax];
bool viz[nMax];
int St[nMax], k;
inline void Dfs(int nod) {
    viz[nod] = 1;
    for(const auto &i : Graf[nod]) {
        if(!viz[i]) {
            Dfs(i);
        }
    }
    St[++k] = nod;
}
int main()
{
    int n, m, x, y;
    f >> n >> m;
    for(int i = 1; i <= m; i++) {
        f >> x >> y;
        Graf[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;
}