# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1107025 | LucaLucaM | Cat in a tree (BOI17_catinatree) | C++17 | 2 ms | 11088 KiB |
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 <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#warning That's not the baby, that's my baby
#define debug(x) #x << " = " << x << '\n'
using ll = long long;
const int INF = 1e9;
const int NMAX = 2e5;
std::vector<int> dp[NMAX + 1];
std::vector<int> g[NMAX + 1];
int depth[NMAX + 1];
int deepest[NMAX + 1];
int n, d;
void join(std::vector<int> &a, const std::vector<int> &b) {
assert((int) a.size() >= (int) b.size());
for (int i = 0; i < (int) b.size() && i + 1 <= d; i++) { // pun la distanta i de b
if (d - 1 - i < (int) a.size()) {
a[d - 1 - i] += b[i];
}
}
}
void dfs(int u) {
deepest[u] = depth[u];
int with = -1;
for (const auto &v : g[u]) {
depth[v] = 1 + depth[u];
dfs(v);
if (deepest[v] > deepest[u]) {
deepest[u] = deepest[v];
with = v;
}
}
if (with == -1) { // frunza
dp[u].resize(1);
dp[u][0] = 1;
return;
}
std::swap(dp[u], dp[with]);
dp[u].insert(dp[u].begin(), 1);
if ((int) dp[u].size() > d) {
dp[u][0] += dp[u][d];
}
for (const auto &v : g[u]) {
if (v != with) {
join(dp[u], dp[v]);
}
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
std::cin >> n >> d;
for (int i = 1; i < n; i++) {
int p;
std::cin >> p;
g[p + 1].push_back(i + 1);
}
dfs(1);
std::cout << *std::max_element(dp[1].begin(), dp[1].end()) << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |