/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200002;
int n, maxd;
int parent[N_MAX];
vector <int> sons[N_MAX];
int depth[N_MAX];
void dfs (int u)
{
depth[u] = 1;
for(int v : sons[u])
{
dfs(v);
depth[u] = max(depth[u], depth[v] + 1);
}
}
int null = 0;
class safe_deque
{
public:
deque <int> dq;
int& operator[] (int index)
{
if(index >= this->dq.size())
return null;
return this->dq[index];
}
void push_back (int val)
{
this->dq.push_back(val);
}
void push_front (int val)
{
this->dq.push_front(val);
}
};
safe_deque dp[N_MAX];
void solve (int u)
{
if(depth[u] == 1)
{
dp[u].push_back(1);
return;
}
for(int v : sons[u])
solve(v);
int secondDepth = -1;
bool firstDepth = false;
for(int v : sons[u])
if(depth[v] + 1 == depth[u] && firstDepth == false)
firstDepth = true;
else
secondDepth = max(secondDepth, depth[v]);
vector <int> vsum(min(maxd / 2, secondDepth + 1) + 1);
vector <int> vmax(min(maxd / 2, secondDepth + 1) + 1);
for(int v : sons[u])
{
int lim = min({maxd / 2, depth[v] + 1, secondDepth + 1});
for(int d = 1; d <= lim; d++)
{
vsum[d] += dp[v][maxd - d - 1];
vmax[d] = max(vmax[d], dp[v][d - 1] - dp[v][maxd - d - 1]);
}
}
int deepSon = -1;
for(int v : sons[u])
if(deepSon == -1 || depth[v] > depth[deepSon])
deepSon = v;
swap(dp[u], dp[deepSon]);
int sum = 1;
for(int v : sons[u])
sum += dp[v][maxd - 1];
dp[u].push_front(sum);
for(int v : sons[u])
if(v != deepSon)
{
for(int d = (maxd + 1) / 2; d <= maxd - 1; d++)
dp[u][d] += dp[v][d - 1];
}
for(int d = 1; d <= min(maxd / 2, secondDepth + 1); d++)
dp[u][d] = vsum[d] + vmax[d];
for(int d = min(maxd - 2, secondDepth + 1); d >= 0; d--)
dp[u][d] = max(dp[u][d], dp[u][d + 1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> maxd;
for(int i = 2; i <= n; i++)
{
cin >> parent[i];
parent[i]++;
}
for(int i = 2; i <= n; i++)
sons[parent[i]].push_back(i);
dfs(1);
solve(1);
cout << dp[1][0] << "\n";
return 0;
}
Compilation message
catinatree.cpp: In member function 'int& safe_deque::operator[](int)':
catinatree.cpp:43:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if(index >= this->dq.size())
| ~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
105 ms |
139628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
105 ms |
139628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
105 ms |
139628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |