#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, d; cin>>n>>d;
vector<int> g[n + 1];
for (int i = 2; i <= n; i++){
int p; cin>>p; p++;
g[p].pb(i);
}
vector<vector<int>> dp(n + 1, vector<int>(d));
function<void(int)> dfs = [&](int v){
for (int i: g[v]){
dfs(i);
}
dp[v][0] = 1;
for (int i: g[v]) dp[v][0] += dp[i][d - 1];
for (int x = 1; x < d; x++){
int sum = 0;
for (int i: g[v]){
sum += dp[i][max(x - 1, d - x - 1)];
}
for (int i: g[v]){
dp[v][x] = max(dp[v][x], (sum - dp[i][max(x - 1, d - x - 1)]) + dp[i][x - 1]);
}
}
};
dfs(1);
int out = 0;
for (int i = 1; i <= n; i++){
for (int j = 0; j < d; j++){
out = max(out, dp[i][j]);
}
}
cout<<out<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |