# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
237236 | IgorI | Cat in a tree (BOI17_catinatree) | C++17 | 280 ms | 524292 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;
typedef long long ll;
const int N = 200000;
const int K = 2000;
int n, d;
int dp[N][K];
int p[N];
vector<int> graph[N];
void dfs(int v)
{
for (auto u : graph[v]) dfs(u);
dp[v][0] = 1;
for (auto u : graph[v]) dp[v][0] += dp[u][d - 1];
for (int j = d; j > 0; j--)
{
if (2 * j >= d)
{
for (auto u : graph[v])
{
dp[v][j] += dp[u][j - 1];
}
}
else
{
int allow = d - j - 1;
dp[v][j] = dp[v][d - j];
int r = 0;
for (auto u : graph[v])
{
r = max(r, dp[u][j - 1] - dp[u][d - j - 1]);
}
dp[v][j] += r;
}
}
for (int j = d - 1; j >= 0; j--) dp[v][j] = max(dp[v][j], dp[v][j + 1]);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> d;
if (d == 1) cout << n, exit(0);
for (int i = 1; i < n; i++)
{
cin >> p[i];
graph[p[i]].push_back(i);
}
if (d < K)
{
dfs(0);
cout << dp[0][0];
}
else
{
cout << 1;
}
}
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... |