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 ll long long
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define left (h<<1),l,(l + r)/2
#define right ((h<<1)|1),(l + r)/2 + 1,r
using namespace std;
const int N = 5e3 + 5;
vector <int> v[N];
int dp[N][N],dpnew[N],K;
int dfs(int x,int par){
dp[x][0] = 1;
int s = 1;
int mx = 0;
for (int to: v[x]){
int y1 = dfs(to,x);
s = max(s,y1 + 1);
for (int i = 0; i <= mx; i++){
for (int j = 0; j <= y1; j++){
int dist = i + j + 1;
if (dist < K) continue;
dpnew[min(i,j + 1)] = max(dpnew[min(i,j + 1)],dp[x][i] + dp[to][j]);
}
}
for (int j = 0; j <= y1; j++)
dpnew[j + 1] = max(dpnew[j + 1],dp[to][j]);
mx=max(mx,y1);
for (int j = 0; j <= mx; j++)
dp[x][j] = max(dp[x][j],dpnew[j]),dpnew[j] = 0;
}
return s;
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n>>K;
for (int i = 2; i <= n; i++){
int par;
cin>>par;
v[par + 1].pb(i);
}
dfs(1,1);
int ans=0;
for (int i = 0; i <= n; i++){
ans=max(ans,dp[1][i]);
}
cout<<ans<<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... |