1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <bits/stdc++.h>
struct Edge { int v; }; using AdjList = std::vector<std::vector<Edge>>;
std::vector<int> toposort(const AdjList& G) { const int N = G.size(); std::vector<bool> vi(N); std::vector<int> topo(N); int i = N; auto dfs = [&](auto& dfs, int u) { if (vi[u]) return; vi[u] = true; for (auto& e : G[u]) dfs(dfs, e.v); topo[--i] = u; }; for (int u = 0; u < N; ++u) dfs(dfs, u); return topo; }
using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(0); int N; cin >> N; AdjList G(N); for (int u = 0, v; u < N; ++u) while (cin >> v, v) G[u].emplace_back(Edge{v - 1}); auto topo = toposort(G); for (int u : topo) cout << u + 1 << ' '; return 0; }
|