constint N = 1e5 + 10; int n, m, deg[N]; std::vector<int> G[N], topo;
booltopoSort(){ std::queue<int> q; for (int i = 1; i <= n; ++i) if (!deg[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); topo.push_back(u); for (int v : G[u]) if (--deg[v] == 0) q.push(v); } return (int)topo.size() == n; }
intmain(){ std::cin.tie(0)->sync_with_stdio(0); std::cin >> n >> m; for (int i = 0, u, v; i < m; ++i) { std::cin >> u >> v; G[u].push_back(v); ++deg[v]; } if (topoSort()) { for (int x : topo) std::cout << x << ' '; std::cout << '\n'; } std::cout.flush(); return0; }
booltopoSort(){ std::priority_queue<int, std::vector<int>, std::greater<int>> q; for (int i = 1; i <= n; ++i) if (!deg[i]) q.push(i); while (!q.empty()) { int u = q.top(); q.pop(); topo.push_back(u); for (int v : G[u]) if (--deg[v] == 0) q.push(v); } return (int)topo.size() == n; }