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>
using namespace std;
int pd[1510][1510],x,d,ans,n;
vector<vector<int>>a;
void calculeaza(int nod)
{
int sum[1510],vmax[1510];
for(int i=0;i<=d;++i)
{
sum[i]=0;
vmax[i]=0;
}
for(auto x:a[nod])
{
calculeaza(x);
for(int i=0;i<=d;++i)
{
sum[i]+=pd[x][i];
if(i+2<=d)
vmax[i]=max(vmax[i],pd[x][i]-pd[x][d-i-2]);
}
}
pd[nod][d+1]=sum[d];
for(int i=d;i>=d/2+1;--i)
{
pd[nod][i]=sum[i-1];
pd[nod][i]=max(pd[nod][i],pd[nod][i+1]);
}
for(int i=d/2;i>=1;--i)
{
pd[nod][i]=vmax[i-1]+sum[d-i-1];
pd[nod][i]=max(pd[nod][i],pd[nod][i+1]);
}
pd[nod][0]=1+sum[d-1];
pd[nod][0]=max(pd[nod][0],pd[nod][1]);
}
int main()
{
cin>>n>>d;
a.resize(n+5);
for(int i=1;i<n;++i)
{
cin>>x;
a[x].push_back(i);
}
calculeaza(0);
for(int i=0;i<=d;++i)
ans=max(ans,pd[0][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... |