#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
catinatree.cpp: In function 'void dfs(int)':
catinatree.cpp:31:17: warning: unused variable 'allow' [-Wunused-variable]
int allow = d - j - 1;
^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5248 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5248 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
24 ms |
17024 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
8 ms |
7040 KB |
Output is correct |
24 |
Correct |
9 ms |
7040 KB |
Output is correct |
25 |
Correct |
7 ms |
4992 KB |
Output is correct |
26 |
Correct |
9 ms |
9088 KB |
Output is correct |
27 |
Correct |
9 ms |
9088 KB |
Output is correct |
28 |
Correct |
11 ms |
11264 KB |
Output is correct |
29 |
Correct |
11 ms |
11136 KB |
Output is correct |
30 |
Correct |
11 ms |
11136 KB |
Output is correct |
31 |
Correct |
11 ms |
11392 KB |
Output is correct |
32 |
Correct |
11 ms |
11040 KB |
Output is correct |
33 |
Correct |
10 ms |
11008 KB |
Output is correct |
34 |
Correct |
10 ms |
10752 KB |
Output is correct |
35 |
Correct |
10 ms |
10752 KB |
Output is correct |
36 |
Correct |
11 ms |
11136 KB |
Output is correct |
37 |
Correct |
12 ms |
11136 KB |
Output is correct |
38 |
Correct |
11 ms |
11392 KB |
Output is correct |
39 |
Correct |
11 ms |
11520 KB |
Output is correct |
40 |
Correct |
11 ms |
11264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5248 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
24 ms |
17024 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
8 ms |
7040 KB |
Output is correct |
24 |
Correct |
9 ms |
7040 KB |
Output is correct |
25 |
Correct |
7 ms |
4992 KB |
Output is correct |
26 |
Correct |
9 ms |
9088 KB |
Output is correct |
27 |
Correct |
9 ms |
9088 KB |
Output is correct |
28 |
Correct |
11 ms |
11264 KB |
Output is correct |
29 |
Correct |
11 ms |
11136 KB |
Output is correct |
30 |
Correct |
11 ms |
11136 KB |
Output is correct |
31 |
Correct |
11 ms |
11392 KB |
Output is correct |
32 |
Correct |
11 ms |
11040 KB |
Output is correct |
33 |
Correct |
10 ms |
11008 KB |
Output is correct |
34 |
Correct |
10 ms |
10752 KB |
Output is correct |
35 |
Correct |
10 ms |
10752 KB |
Output is correct |
36 |
Correct |
11 ms |
11136 KB |
Output is correct |
37 |
Correct |
12 ms |
11136 KB |
Output is correct |
38 |
Correct |
11 ms |
11392 KB |
Output is correct |
39 |
Correct |
11 ms |
11520 KB |
Output is correct |
40 |
Correct |
11 ms |
11264 KB |
Output is correct |
41 |
Runtime error |
280 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
42 |
Halted |
0 ms |
0 KB |
- |