This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 2e5 + 5;
vector<int> ad[mxN];
pair<int, int> dp[mxN];
int n, d;
void dfs(int u) {
dp[u] = {0, d};
int s = 0;
vector<int> vv;
for (int v : ad[u]) {
dfs(v);
s += dp[v].first;
vv.push_back(dp[v].second + 1);
}
sort(vv.begin(), vv.end());
// debug(vv);
for (int i = 0; i < (int) vv.size(); i++) {
int j = lower_bound(vv.begin(), vv.end(), d - vv[i]) - vv.begin();
int cnt;
if (j > i) {
cnt = j - 1;
} else {
cnt = i;
}
dp[u] = max(dp[u], {s - cnt, vv[i]});
}
if (dp[u].second >= d) dp[u].first++, dp[u].second = 0;
// debug(u, dp[u]);
}
void testCase() {
cin >> n >> d;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
ad[p].push_back(i);
}
dfs(0);
cout << dp[0].first << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |