Alex_tz307

TopSortMinLex

Sep 11th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MAXN 100005
  6.  
  7. typedef vector < int > Vec;
  8. int N, M, deg[MAXN], nr;
  9. Vec G[MAXN];
  10. priority_queue < int > Q;
  11.  
  12. inline void read () {
  13.     int a, b;
  14.     cin >> N >> M;
  15.     while (M  --) {
  16.         cin >> a >> b;
  17.         G[a].emplace_back (b);
  18.         deg[b] ++;
  19.     }
  20.     for (int i = 1; i <= N; ++i) if (deg[i] == 0) Q.push (-i);
  21. }
  22.  
  23. inline void solve () {
  24.     Vec::iterator it;
  25.     while (Q.size()) {
  26.         nr = -Q.top();
  27.         Q.pop();
  28.         cout << nr << " ";
  29.         for (it = G[nr].begin(); it != G[nr].end(); ++it) {
  30.             deg[*it] --;
  31.             if (deg[*it] == 0) Q.push (-*it);
  32.         }
  33.     }
  34. }
  35.  
  36. int main () {
  37.     read();
  38.     solve();
  39.     return 0;
  40. }
Add Comment
Please, Sign In to add comment