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>
#define f first
#define s second
using namespace std;
const int N = 1569;
int n, d, dp[N][N];
vector<int> g[N];
void dfs(int v, int p) {
vector<int> ch;
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
ch.push_back(u);
}
vector<int> sum(N);
for (auto u : ch) {
for (int j = 0;j <= d;j++)
sum[j] += dp[u][j];
}
for (int j = 1;j <= d;j++) {
for (auto u : ch) {
int x = d - j;
if (x < j) {
int sm = sum[j - 1] - dp[u][j - 1];
dp[v][j] = max(dp[v][j], sm + dp[u][j - 1]);
}
else {
int sm = sum[x - 1] - dp[u][x - 1];
dp[v][j] = max(dp[v][j], sm + dp[u][j - 1]);
}
}
}
dp[v][0] = 1;
for (auto u : ch)
dp[v][0] += dp[u][d - 1];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> d;
for (int i = 2;i <= n;i++) {
int x;
cin >> x;
++x;
g[x].push_back(i);
g[i].push_back(x);
}
dfs(1, 0);
int ans = 0;
for (int i = 0;i <= d;i++)
ans = max(ans, dp[1][i]);
cout << ans;
}
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... |