#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, d;
cin >> n >> d;
vector<vector<int>> adj(n);
for (int i = 1, p; i < n; ++i) {
cin >> p;
adj[p].push_back(i);
}
auto dfs = [&](auto &&self, int u) -> deque<int> {
deque<int> dp(n);
for (int &i : adj[u]) {
deque<int> ndp = self(self, i);
ndp.push_front(0);
ndp.pop_back();
deque<int> nndp(n);
for (int i = 0; i < ndp.size(); ++i) {
nndp[i] = max(dp[i], ndp[i]);
}
for (int i = 0; i < dp.size(); ++i) {
for (int j = 0; j < ndp.size(); ++j) {
nndp[min(i, j)] = max(nndp[min(i, j)], dp[i] + ndp[j]);
}
}
dp = nndp;
}
dp[0] = max(dp[0], dp[d] + 1);
return dp;
};
deque<int> dp = dfs(dfs, 0);
cout << *max_element(dp.begin(), dp.end()) << '\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... |