#include <bits/stdc++.h>
using namespace std;
const int nx=1505;
int n, x, k, dp[nx][nx];
vector<int> d[nx];
void dfs(int u, int p)
{
dp[u][0]=1;
for (auto v:d[u]) if (v!=p) dfs(v, u), dp[u][0]+=dp[v][k-1];
for (int i=1; i<=k; i++)
{
if (k-i-1>=i-1)
{
int tmp=0;
for (auto v:d[u]) if (v!=p) tmp+=dp[v][k-i-1];
for (auto v:d[u]) if (v!=p) dp[u][i]=max(dp[u][i], tmp-dp[v][k-i-1]+dp[v][i-1]);
}
}
for (int i=k-1; i>=0; i--) dp[u][i]=max(dp[u][i], dp[u][i+1]);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k;
for (int i=1; i<n; i++) cin>>x, d[i].push_back(x), d[x].push_back(i);
dfs(0, 0);
cout<<dp[0][0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |