# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
823388 | serifefedartar | Cat in a tree (BOI17_catinatree) | C++17 | 139 ms | 19952 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 <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000009
#define LOGN 20
#define MAXN 2005
vector<vector<int>> graph;
int N, D;
int dp[MAXN][MAXN];
void solve(int node) {
dp[node][0] = 1;
for (auto u : graph[node]) {
solve(u);
dp[node][0] += dp[u][D-1];
}
for (int dist = 1; dist <= N; dist++) {
if (dist * 2 >= D) {
for (auto u : graph[node])
dp[node][dist] += dp[u][dist-1];
} else {
int complement = D - dist - 1;
vector<int> pref(N+1, 0), suff(N+2, 0);
for (int i = 1; i <= graph[node].size(); i++)
pref[i] = pref[i-1] + dp[graph[node][i-1]][complement];
for (int i = graph[node].size(); i >= 1; i--)
suff[i] = suff[i+1] + dp[graph[node][i-1]][complement];
for (int i = 0; i < graph[node].size(); i++)
dp[node][dist] = max(dp[node][dist], pref[i] + suff[i+2] + dp[graph[node][i]][dist-1]);
}
}
for (int i = N; i >= 1; i--)
dp[node][i-1] = max(dp[node][i-1], dp[node][i]);
}
int main() {
fast
cin >> N >> D;
graph = vector<vector<int>>(N, vector<int>());
for (int i = 1; i < N; i++) {
int parent; cin >> parent;
graph[parent].push_back(i);
}
solve(0);
cout << dp[0][0] << "\n";
}
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... |