Cod sursa(job #2614722)

Utilizator MocalinnoMoca Andrei Catalin Mocalinno Data 12 mai 2020 16:26:01
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>
#define DAU  ios::sync_with_stdio(false); fin.tie(0); fout.tie(0);
#define PLEC fin.close(); fout.close(); return 0;
using namespace std;
const string problem("sortaret");
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");
using VI  = vector<int>;
using VVI = vector<VI>;
using VB  = vector<bool>;
VVI g;
VB viz;
stack<int> st;
inline void DFS(int x) {
    for (const int& y : g[x])
        if (!viz[y])
            viz[y] = true, DFS(y);
    st.emplace(x);
}
int n, m, x, y;
int main() {
    DAU
    fin >> n >> m;
    g = VVI(n + 1);
    for (int i = 1; i <= m; ++i) {
        fin >> x >> y;
        g[x].emplace_back(y);
    }
    viz = VB(n + 1);
    for (int i = 1; i <= n; ++i)
        if (!viz[i])
            viz[i] = true, DFS(i);
    while (!st.empty())
        fout << st.top() << ' ', st.pop();
    PLEC
}