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;
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long
const int MXN = 2e5 + 5, INF = 1e9 + 5;
int N, D;
vector<int> g[MXN];
deque<int> dfs(int u, int p) {
deque<int> res = {1};
for (const auto &v : g[u]) {
if (v == p) continue;
deque<int> merge = dfs(v, u);
merge.push_front(merge.front());
if (sz(merge) > sz(res))
swap(merge, res);
for (int i = 0; i < sz(merge); i++) {
int val1 = merge[i] + (D - i < sz(res) ? res[max(i, D - i)] : 0);
int val2 = res[i] + (D - i < sz(merge) ? merge[max(i, D - i)] : 0);
res[i] = max({res[i], val1, val2});
}
for (int i = sz(merge) - 2; i >= 0; i--)
res[i] = max(res[i], res[i + 1]);
}
return res;
}
void solve() {
cin >> N >> D;
for (int i = 1; i < N; i++) {
int x;
cin >> x;
g[x].push_back(i);
g[i].push_back(x);
}
cout << dfs(0, -1).front();
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int TC = 1;
// cin >> TC;
while (TC--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |