Cod sursa(job #2575603)

Utilizator nTropicManescu Bogdan Constantin nTropic Data 6 martie 2020 14:36:52
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

const int len = 100005;
int n, m, x, y;
vector<int> g[len], sol;
bool seen[len];

void dfs(int node) {
    seen[node] = true;
    for (auto& it : g[node])
        if (!seen[it])
            dfs(it);
    sol.push_back(node);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    freopen("sortaret.in", "r", stdin);
    freopen("sortaret.out", "w", stdout);

    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> x >> y;
        g[x].push_back(y);
    }

    for (int i = 1; i <= n; i++)
        if (!seen[i])
            dfs(i);

    for (auto& it : sol)
        cout << it << " ";
}