Advertisement
VladNitu

CourseScheduleVlad

Jun 9th, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include<ext/pb_ds/assoc_container.hpp>
  3. #include<ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6. template <typename T>
  7. using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  8.  
  9.  
  10. #define NMAX (int)(1000)
  11. #define INF (int)(1e9)
  12. #define ll long long
  13. #define mkp make_pair
  14. #define mkt make_tuple
  15. #define lsb(x) (x & -x)
  16.  
  17. void __print(int x) {cerr << x;}
  18. void __print(long x) {cerr << x;}
  19. void __print(long long x) {cerr << x;}
  20. void __print(unsigned x) {cerr << x;}
  21. void __print(unsigned long x) {cerr << x;}
  22. void __print(unsigned long long x) {cerr << x;}
  23. void __print(float x) {cerr << x;}
  24. void __print(double x) {cerr << x;}
  25. void __print(long double x) {cerr << x;}
  26. void __print(char x) {cerr << '\'' << x << '\'';}
  27. void __print(const char *x) {cerr << '\"' << x << '\"';}
  28. void __print(const string &x) {cerr << '\"' << x << '\"';}
  29. void __print(bool x) {cerr << (x ? "true" : "false");}
  30.  
  31. template<typename T, typename V, typename W>
  32. void __print(const std::tuple<T, V, W> &x) {cerr << '{'; __print(std::get<0>(x)); cerr << ','; __print(std::get<1>(x)); cerr << ','; __print(std::get<2>(x)); cerr << '}';}
  33. template<typename T, typename V>
  34. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  35. template<typename T>
  36. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  37. void _print() {cerr << "]\n";}
  38. template <typename T, typename... V>
  39. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  40. #ifndef ONLINE_JUDGE
  41. #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
  42. #else
  43. #define dbg(x...)
  44. #endif
  45.  
  46.  
  47. int T, N, M;
  48. int k;
  49. int x, y;
  50. int in[NMAX];
  51.  
  52. vector<int> G[NMAX];
  53. vector<int> parent[NMAX];
  54. vector<int> doneInQuarter;
  55. vector<int> dp;
  56. bitset<NMAX> viz;
  57.  
  58.  
  59. void maxPath(int nod) { // O(N+M) DP; dp[x] = length of longest path starting at `x`
  60.  
  61.     viz[nod] = true;
  62.     for (const int& neigh: G[nod]) {
  63.         if (!viz[neigh])
  64.             maxPath(neigh);
  65.         dp[nod] = max(dp[nod], dp[neigh] + 1);
  66.     }
  67.     return ;
  68. }
  69.  
  70.  
  71. int main() {
  72.  
  73. //    freopen("instances/large19.in", "r", stdin);
  74. //    freopen("instances/large19.res", "w", stdout);
  75.  
  76.  
  77.  
  78.     cin >> N >> M;
  79.     dp = vector<int>(N + 1);
  80.     doneInQuarter = vector<int>(N + 1);
  81.  
  82.     for (int i = 0; i < M; ++i) {
  83.         cin >> x >> y;
  84.         in[y] ++;
  85.         G[x].push_back(y);
  86.         parent[y].push_back(x);
  87.     }
  88.     cin >> k;
  89.  
  90.  
  91.     for (int x = 1; x <= N; ++x)
  92.         if (!viz[x])
  93.             maxPath(x);
  94.  
  95.  
  96.  
  97.     priority_queue<pair<int, int>> pq; // {maxPath, node}
  98.     for (int x = 1; x <= N; ++x)
  99.         if (in[x] == 0)
  100.             pq.push({dp[x], x});
  101.  
  102.  
  103.  
  104.     int quarters = 0;
  105.  
  106.     while (!pq.empty()) {
  107.  
  108.         queue<pair<int, int>> Q;
  109.         quarters ++;
  110.         int sz = pq.size();
  111.  
  112.         for (int it = 0; it < min(sz, k); ++it) {
  113.  
  114.             const auto [maxPath, x] = pq.top();
  115.             pq.pop();
  116.  
  117.             doneInQuarter[x] = quarters;
  118.  
  119.             for (const int &y: G[x]) {
  120.  
  121.                 in[y]--;
  122.                 if (in[y] == 0) {
  123.                     Q.push({dp[y], y});
  124.                 }
  125.             }
  126.         }
  127.         while (!Q.empty()) {
  128.             pq.push(Q.front());
  129.             Q.pop();
  130.         }
  131.  
  132.     }
  133.  
  134.     cout << quarters;
  135.     return 0;
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement