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 <iostream>
#include <vector>
using namespace std;
const int MAXN = 1580;
vector<int> edg[MAXN];
int depth[MAXN];
int dp[MAXN][MAXN];
signed main() {
int n, d;
cin >> n >> d;
for (int i = 1; i < n; ++i) {
int rt;
cin >> rt;
edg[rt].push_back(i);
depth[i] = depth[rt] + 1;
}
for (int i = n - 1; i >= 0; --i) {
dp[i][depth[i]] = 1;
if (d + depth[i] < n) {
for (auto u : edg[i]) {
dp[i][depth[i]] += dp[u][d + depth[i]];
}
}
for (int t = depth[i] + 1; t < n; ++t) {
int d0 = min(n, max(t, depth[i] * 2 + d - t));
int mx = 0;
for (auto u : edg[i]) {
dp[i][t] += dp[u][d0];
mx = max(mx, dp[u][t] - dp[u][d0]);
}
dp[i][t] += mx;
}
for (int t = n - 2; t >= 0; --t) {
dp[i][t] = max(dp[i][t], dp[i][t + 1]);
}
//cout << i << ": ";
//for (int j = 0; j < n; ++j)
//cout << dp[i][j] << ' ';
//cout << '\n';
}
cout << dp[0][0] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |