#include <iostream> #include <algorithm> #include <queue> #include <math.h> #include <climits> #include <map> using namespace std; int degree[32001]; vector<int> buffer[32001]; int main(){ cin.tie(NULL); ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (auto i =0; i<m; i++){ int from, to; cin >> from >> to; buffer[from-1].push_back(to-1); degree[to-1]++; } priority_queue<int, vector<int>, greater<int>> q; for (auto i =0; i<n; i++){ if (degree[i] == 0){ // cout << i << 'a' << '\n'; q.push(i); } }
while (!q.empty()){ int top = q.top(); cout << top+1 << ' '; q.pop(); for (int to : buffer[top]){ degree[to]--; if (degree[to] == 0){ q.push(to); } } }