Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- using namespace std;
- int main() {
- priority_queue<int> pq;
- vector<int> ind;
- vector<vector<int> > a;
- int n, m, x, y;
- int i, j;
- cin >> n >> m;
- ind.resize(n + 1);
- a.resize(n + 1);
- for (i = 0; i < m; i++) {
- cin >> x >> y;
- a[x].push_back(y);
- ind[y]++;
- }
- for (i = 1; i <= n; i++) {
- if (ind[i] == 0) {
- pq.push(-i);
- }
- }
- for (i = 0; i < n; i++) {
- x = -pq.top();
- pq.pop();
- cout << x << " ";
- for (j = 0; j < a[x].size(); j++) {
- y = a[x][j];
- ind[y]--;
- if (ind[y] == 0) {
- pq.push(-y);
- }
- }
- }
- cout << '\n';
- return 0;
- }
Add Comment
Please, Sign In to add comment