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;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 2e5 + 10;
deque <int> dq[N];
int d;
vector <int> ed[N];
void merge(deque <int> &a, deque <int> &b) {
for (int i = 0; i < sz(a); i++) {
if (d - i < sz(b))
b[i] = max(b[i], a[i] + b[d - i]);
}
for (int i = sz(a) - 2; i >= 0; i--)
b[i] = max(b[i], b[i + 1]);
}
void dfs(int u) {
dq[u].push_back(1);
for (auto x : ed[u]) {
dfs(x);
if (sz(dq[u]) < sz(dq[x])) {
dq[x].push_front(0);
swap(dq[u], dq[x]);
} else {
dq[u].push_front(0);
}
merge(dq[x], dq[u]);
}
}
ll solve() {
int n;
cin >> n >> d;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
ed[x].pb(i);
}
dfs(0);
return dq[0][0];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
//solve();
cout << solve() << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |