Guest User

Untitled

a guest
Jul 17th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main() {
  7. priority_queue<int> pq;
  8. vector<int> ind;
  9. vector<vector<int> > a;
  10. int n, m, x, y;
  11. int i, j;
  12.  
  13. cin >> n >> m;
  14.  
  15. ind.resize(n + 1);
  16. a.resize(n + 1);
  17.  
  18. for (i = 0; i < m; i++) {
  19. cin >> x >> y;
  20. a[x].push_back(y);
  21. ind[y]++;
  22. }
  23.  
  24. for (i = 1; i <= n; i++) {
  25. if (ind[i] == 0) {
  26. pq.push(-i);
  27. }
  28. }
  29.  
  30. for (i = 0; i < n; i++) {
  31. x = -pq.top();
  32. pq.pop();
  33. cout << x << " ";
  34. for (j = 0; j < a[x].size(); j++) {
  35. y = a[x][j];
  36. ind[y]--;
  37. if (ind[y] == 0) {
  38. pq.push(-y);
  39. }
  40. }
  41. }
  42. cout << '\n';
  43.  
  44.  
  45. return 0;
  46. }
Add Comment
Please, Sign In to add comment