#include <bits/stdc++.h>
using namespace std;
int const N=1505;
vector<int> adj[N];
int opt[N][N];
int n,D;
int sm[N];
void dfs(int node){
for(int i=0;i<D;i++)
sm[i]=0;
for(auto i:adj[node])
dfs(i);
for(auto i:adj[node])
for(int k=0;k<D;k++)
sm[k]+=opt[i][k];
// cout<<"node := "<<node<<endl;
//mark the node
for(int k=D-1;k>=1;k--){
for(int i:adj[node]){
int cal=opt[i][k-1];
cal+=(sm[max(k,D-k)-1]-opt[i][max(k,D-k)-1]);
// cout<<k<<' '<<i<<' '<<(sm[max(k,D-k)-1]-opt[i][max(k,D-k)-1])<<endl;
opt[node][k]=max(opt[node][k],cal);
}
opt[node][k]=max(opt[node][k],opt[node][k+1]);
}
opt[node][0]=max(1+sm[D-1],opt[node][1]);
// for(int i=0;i<D;i++)
// cout<<opt[node][i]<<' ';
// cout<<endl;
}
int main(){
cin>>n>>D;
for(int i=1;i<n;i++){
int a;
cin>>a;
adj[a].push_back(i);
}
if(D>=n){
cout<<1<<endl;
return 0;
}
dfs(0);
cout<<opt[0][0]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |