Advertisement
pb_jiang

CF1739D WA

Jan 6th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  5. template <typename... Args> void logger(string vars, Args &&... values)
  6. {
  7.     cerr << vars << " = ";
  8.     string delim = "";
  9.     (..., (cerr << delim << values, delim = ", "));
  10.     cerr << endl;
  11. }
  12.  
  13. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  14. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  15. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  16. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  17.  
  18. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  19.  
  20. using ll = long long;
  21. using pii = pair<int, int>;
  22. using vl = vector<ll>;
  23. using vi = vector<int>;
  24.  
  25. int main(int argc, char **argv)
  26. {
  27.     int t;
  28.     cin >> t;
  29.     while (t--) {
  30.         int n, k;
  31.         cin >> n >> k;
  32.         vi p(n + 1);
  33.         vector<int> tree[200003];
  34.         for (int i = 2; i <= n; ++i) {
  35.             cin >> p[i];
  36.             tree[p[i]].push_back(i);
  37.         }
  38.         int lb = 1, ub = 200003, cnt = ub - lb, it, step;
  39.         int ans = 0;
  40.         function<int(int, int, int)> dfs = [&tree, &ans, &dfs](int u, int fa, int len) {
  41.             int unfinish = 1;
  42.             for (auto v : tree[u]) {
  43.                 if (v == fa)
  44.                     continue;
  45.                 int zlen = dfs(v, u, len);
  46.                 unfinish = max(zlen, unfinish);
  47.             }
  48.             unfinish += 1;
  49.             if (unfinish + 1 == len)
  50.                 unfinish = 0, ans += 1;
  51.             return unfinish == INT_MIN ? 1 : unfinish;
  52.         };
  53.         auto check = [&ans, &dfs](int height) {
  54.             ans = 0;
  55.             dfs(1, -1, height);
  56.             return ans;
  57.         };
  58.         while (cnt) {
  59.             it = lb, step = cnt / 2, it += step;
  60.             if (check(it) > k) {
  61.                 lb = it + 1;
  62.                 cnt = cnt - (step + 1);
  63.             } else {
  64.                 cnt = step;
  65.             }
  66.         }
  67.         cout << lb << endl;
  68.     }
  69.     return 0;
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement