#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#include <cassert>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using i64 = long long;
#define N 200860
int n, d, l[N], h[N], alloc = 1, dp[N][2], t[N];
pair<int, int> g[N<<1];
void upd(int p, int k) { p += 100;for (; p ; p-=p&-p) t[p] += k; }
int qry(int p) { p += 100; int z = 0; for (; p < (N<<1); p+=p&-p) z += t[p]; return z; }
void add_edge(int u, int v)
{
g[alloc] = {v, h[u]}; h[u] = alloc++;
}
void dfs(int u, int p, int dep)
{
l[u] = dep;
int base = 0;
for (auto j = h[u]; j; j = g[j].second)
{
int v = g[j].first;
if (v == p) continue;
dfs(v, u, dep+1), base += dp[v][0] - 1;
}
vector<int> depmap;
for (auto j = h[u]; j; j = g[j].second)
if (g[j].first != p)
upd(dp[g[j].first][1], 1), depmap.emplace_back(dp[g[j].first][1]);
sort(ALL(depmap));
{
int sel = base + 1 + qry(d + dep);
if (dp[u][0] < sel) dp[u][0] = sel, dp[u][1] = dep;
else if (dp[u][0] == sel && dep > dp[u][1]) dp[u][1] = dep;
}
for (auto j = h[u]; j; j = g[j].second)
{
int v = g[j].first;
if (v == p) continue;
int delta = dp[v][1] - dep;
int safe = (d+1)/2, sel;
int subdep = dp[v][1];
if (delta >= safe)
{
sel = base + qry(dep + safe);
auto it = lower_bound(ALL(depmap), dep+safe);
subdep = *it;
}
else
{
assert(safe < d - delta);
upd(dp[g[j].first][1], -1);
sel = base + 1 + qry(dep + max(safe, d - delta));
upd(dp[g[j].first][1], 1);
}
if (dp[u][0] < sel) dp[u][0] = sel, dp[u][1] = subdep;
else if (dp[u][0] == sel && subdep > dp[u][1]) dp[u][1] = subdep;
}
for (auto j = h[u]; j; j = g[j].second)
if (g[j].first != p) upd(dp[g[j].first][1], -1);
}
int main()
{
ShinLena;
cin >> n >> d;
for (int i = 1, v; i < n; ++i) cin >> v, add_edge(v, i), add_edge(i, v);
dfs(1, 1, 1);
for (int i = 0; i < n; ++i)
{
// cerr << dp[i][0] << ' '<< dp[i][1] << ' ' <<l[i] <<endl;
}
cout << dp[1][0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
516 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
516 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
516 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |