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 fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 3e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int n;
int k;
int a[mxN];
int res = 0;
vector<int> adj[mxN];
deque<ll> dfs(int u, int p)
{
deque<ll> res;
res.push_front(1);
for(int v : adj[u]) {
if(v == p) continue;
deque<ll> add = dfs(v, u);
add.push_front(add.front());
if(sz(res) < sz(add)) swap(res, add);
deque<ll> combined = add;
for(int x = 0; x < sz(add); x++) {
int y = max(x, k - x);
if(y < sz(res))
maximize(combined[x], add[x] + res[y]);
if(y < sz(add))
maximize(combined[x], res[x] + add[y]);
}
int maximum = 0;
for(int i = sz(add) - 1; i >= 0; i--) {
maximize(maximum, combined[i]);
maximize(maximum, add[i]);
maximize(res[i], maximum);
}
}
return res;
}
// dp[u][i] = dp[u][i + 1]
// dp[u][min(i, j + 1)] = dp[u][i] + dp[v][j];
// dp[u][i] = dp[u][i] + sufchild[v][j]
// dp[u][j + 1] = sufroot[u][i] + dp[v][j + 1]
void solve()
{
cin >> n >> k;
//for(int i = 1; i <= n; i++) cin >> a[i];
for(int u = 2; u <= n; u++) {
int v;
cin >> v;
++v;
adj[u].pb(v);
adj[v].pb(u);
}
deque<ll> res = dfs(1, -1);
cout << res.front();
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("task.inp", "r", stdin);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
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... |