This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC Optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pairll;
typedef pair<ll,pairll>pairlll;
typedef pair<pairll,pairll>pairllll;
typedef long double ld;
typedef pair<ll,string>pairls;
#define INF 1000000000000007
#define MOD 1000000007
#define pb push_back
#define fr first
#define sc second
#define endl '\n'
ll n,m,t[1507][1507],res;
vector<ll>v[1507];
void S(ll x,ll y){
for(int i=0;i<int(v[x].size());i++){
if(v[x][i]!=y)S(v[x][i],x);
}
for(int i=m-1;i>=1;i--){
if(i*2<m){
t[x][i]=0;
ll h=0;
for(int j=0;j<int(v[x].size());j++){
if(v[x][j]!=y)h+=t[v[x][j]][m-i-1];
}
for(int j=0;j<int(v[x].size());j++){
if(v[x][j]!=y){
t[x][i]=max(t[x][i],h-t[v[x][j]][m-i-1]+t[v[x][j]][i-1]);
}
}
}else{
t[x][i]=0;
for(int j=0;j<int(v[x].size());j++){
if(v[x][j]!=y)t[x][i]+=t[v[x][j]][i-1];
}
}
t[x][i]=max(t[x][i],t[x][i+1]);
}
t[x][0]=1;
for(int i=0;i<int(v[x].size());i++){
if(v[x][i]!=y)t[x][0]+=t[v[x][i]][m-1];
}
t[x][0]=max(t[x][0],t[x][1]);
return ;
}
int main() {
cin>>n>>m;
m=min(n,m);
for(int i=1;i<n;i++){
ll l;
cin>>l;
v[l].pb(i);
v[i].pb(l);
}
S(0,0);
res=0;
for(int i=0;i<m;i++){
res=max(res,t[0][i]);
}
cout<<res<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |