#include <bits/stdc++.h>
namespace std {
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
} // namespace std
struct heavy_light_decomposition {
std::vector<int> par, dep, hed, tin;
heavy_light_decomposition(int R, std::vector<std::vector<int>> T) {
int n = (int)T.size(), time = 0;
par.resize(n), dep.resize(n);
hed.resize(n), tin.resize(n);
par[R] = -1, dep[R] = 0, hed[R] = R;
std::y_combinator([&](auto self, int u) -> int {
int sub = 1, mxs = 0;
for (int &v : T[u]) {
if (v == par[u])
continue;
par[v] = u;
dep[v] = dep[u] + 1;
int t = self(v);
sub += t;
if (mxs < t) {
mxs = t;
std::swap(T[u].front(), v);
}
}
return sub;
})(R);
std::y_combinator([&](auto self, int u) -> void {
tin[u] = time++;
for (int v : T[u]) {
if (v == par[u])
continue;
hed[v] = v == T[u].front() ? hed[u] : v;
self(v);
}
})(R);
}
int lowest_common_ancestor(int u, int v) {
for (; hed[u] != hed[v]; u = par[hed[u]])
if (dep[hed[u]] < dep[hed[v]])
std::swap(u, v);
return dep[u] < dep[v] ? u : v;
}
int distance(int u, int v) { return dep[u] + dep[v] - 2 * dep[lowest_common_ancestor(u, v)]; }
};
class centroid_decomposition {
int n;
std::vector<std::vector<int>> tree;
std::vector<int> parent, weight;
std::vector<bool> block;
int initialize_weight(int u, int p) {
weight[u] = 1;
for (int v : tree[u]) {
if (p == v || block[v])
continue;
weight[u] += initialize_weight(v, u);
}
return weight[u];
}
int find_centroid(int w, int u, int p) {
for (int v : tree[u]) {
if (p == v || block[v])
continue;
if (2 * weight[v] > w)
return find_centroid(w, v, u);
}
return u;
}
void initialize_centroids(int c, int p) {
int w = initialize_weight(c, -1);
c = find_centroid(w, c, -1);
parent[c] = p;
block[c] = true;
for (int v : tree[c]) {
if (block[v])
continue;
initialize_centroids(v, c);
}
}
public:
centroid_decomposition(const std::vector<std::vector<int>> &_tree) : n((int)_tree.size()), tree(_tree) {
parent.resize(n);
weight.resize(n);
block.assign(n, false);
initialize_centroids(0, -1);
}
int operator[](int i) { return parent[i]; }
};
int main() {
using namespace std;
int N, D;
cin >> N >> D;
vector<vector<int>> T(N);
for (int i = 1; i < N; i++) {
int p;
cin >> p;
T[i].push_back(p);
T[p].push_back(i);
}
heavy_light_decomposition hld(0, T);
centroid_decomposition cd(T);
vector<int> ord(N);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&hld](int a, int b) {
return hld.dep[a] > hld.dep[b];
});
vector<int> ans;
vector<int> dis(N, (int)1e9);
for (int s : ord) {
int cur = (int)1e9;
for (int t = s; t != -1; t = cd[t])
cur = min(cur, dis[t] + hld.distance(s, t));
if (cur >= D) {
ans.push_back(s);
for (int t = s; t != -1; t = cd[t]) {
dis[t] = min(dis[t], hld.distance(s, t));
}
}
}
cout << ans.size() << '\n';
// for (int u : ans)
// cout << u + 1 << ' ';
// cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
428 KB |
Output is correct |
27 |
Correct |
1 ms |
428 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
432 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
428 KB |
Output is correct |
27 |
Correct |
1 ms |
428 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
432 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
134 ms |
28920 KB |
Output is correct |
42 |
Correct |
132 ms |
15948 KB |
Output is correct |
43 |
Correct |
117 ms |
15000 KB |
Output is correct |
44 |
Correct |
123 ms |
15120 KB |
Output is correct |
45 |
Correct |
118 ms |
15044 KB |
Output is correct |
46 |
Correct |
339 ms |
30892 KB |
Output is correct |
47 |
Correct |
327 ms |
29952 KB |
Output is correct |
48 |
Correct |
317 ms |
29904 KB |
Output is correct |
49 |
Correct |
350 ms |
30020 KB |
Output is correct |
50 |
Correct |
71 ms |
15692 KB |
Output is correct |
51 |
Correct |
75 ms |
15652 KB |
Output is correct |
52 |
Correct |
71 ms |
15668 KB |
Output is correct |
53 |
Correct |
183 ms |
31016 KB |
Output is correct |
54 |
Correct |
186 ms |
31020 KB |
Output is correct |
55 |
Correct |
195 ms |
31068 KB |
Output is correct |
56 |
Correct |
2 ms |
596 KB |
Output is correct |
57 |
Correct |
18 ms |
4388 KB |
Output is correct |
58 |
Correct |
97 ms |
19768 KB |
Output is correct |
59 |
Correct |
246 ms |
35152 KB |
Output is correct |
60 |
Correct |
131 ms |
29568 KB |
Output is correct |
61 |
Correct |
199 ms |
28252 KB |
Output is correct |