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;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
const int maxn = 200010;
const int INF = LLONG_MAX/2;
const int mod = 1e9+7;
int n, K;
//https://codeforces.com/blog/entry/70822
struct dat {
deque <int> dp;
dat (int v = 0) {
dp = {v};
}
int size() const {
return (int) dp.size();
}
};
vector <int> adjlist[maxn];
vector <dat> ans;
void extend(dat &root) {
root.dp.push_front(root.dp.front());
}
void attach(dat &a, dat &b) {
if (a.size() < b.size()) { //b is the smaller one
swap(a, b);
}
//We will at most modify the first b.dp.size values of a
vector <int> v = vector<int>(all(b.dp));
for (int x = 0; x < b.size(); x++) {
int miny = max(K - x, x);
if (miny < b.size()) {
v[x] = max(v[x], a.dp[x] + b.dp[miny]);
}
if (miny < a.size()) {
v[x] = max(v[x], b.dp[x] + a.dp[miny]);
}
}
for (int i = v.size() - 1; i >= 0; i--) {
if (i+1 < v.size()) v[i] = max(v[i], v[i+1]);
a.dp[i] = max(a.dp[i], v[i]);
}
}
//Return reference to dat for x
dat& dpf(int x, int p) {
dat &res = ans[x]; //reference to thingy
res = dat(1);
for (auto i : adjlist[x]) if (i != p) {
dat &u = dpf(i,x);
extend(u);
attach(res, u);
}
return res;
}
int32_t main() {
FAST
// ifstream cin("input.txt");
cin >> n >> K;
for (int i = 2; i <= n; i++) {
int p; cin >> p;
p++;
adjlist[p].push_back(i);
}
ans.assign(n+1, {});
cout << dpf(1, -1).dp.front();
}
Compilation message (stderr)
catinatree.cpp: In function 'void attach(dat&, dat&)':
catinatree.cpp:55:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | if (i+1 < v.size()) v[i] = max(v[i], v[i+1]);
| ~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |