Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
- template <typename... Args> void logger(string vars, Args &&... values)
- {
- cerr << vars << " = ";
- string delim = "";
- (..., (cerr << delim << values, delim = ", "));
- cerr << endl;
- }
- template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
- template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
- template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
- template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using vl = vector<ll>;
- using vi = vector<int>;
- int main(int argc, char **argv)
- {
- int t;
- cin >> t;
- while (t--) {
- int n, k;
- cin >> n >> k;
- vi p(n + 1);
- vector<int> tree[200003];
- for (int i = 2; i <= n; ++i) {
- cin >> p[i];
- tree[p[i]].push_back(i);
- }
- int lb = 1, ub = 200003, cnt = ub - lb, it, step;
- int ans = 0;
- function<int(int, int, int)> dfs = [&tree, &ans, &dfs](int u, int fa, int len) {
- int unfinish = 1;
- for (auto v : tree[u]) {
- if (v == fa)
- continue;
- int zlen = dfs(v, u, len);
- unfinish = max(zlen, unfinish);
- }
- unfinish += 1;
- if (unfinish + 1 == len)
- unfinish = 0, ans += 1;
- return unfinish == INT_MIN ? 1 : unfinish;
- };
- auto check = [&ans, &dfs](int height) {
- ans = 0;
- dfs(1, -1, height);
- return ans;
- };
- while (cnt) {
- it = lb, step = cnt / 2, it += step;
- if (check(it) > k) {
- lb = it + 1;
- cnt = cnt - (step + 1);
- } else {
- cnt = step;
- }
- }
- cout << lb << endl;
- }
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement