#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 4;
const int INF = 1e9;
const int N = 18;
int n, d, h[maxn], up[maxn][20], par[maxn], mn[maxn];
vector<int> adj[maxn], nodes;
bool used[maxn];
void dfs(int u, int p) {
up[u][0] = p;
for (int j = 1; j <= N; j++)
up[u][j] = up[up[u][j - 1]][j - 1];
for (int v : adj[u])
if (v != p) {
h[v] = h[u] + 1;
dfs(v, u);
}
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
for (int j = N; j >= 0; j--)
if (k >> j & 1)
u = up[u][j];
if (u == v) return u;
for (int j = N; j >= 0; j--)
if (up[u][j] != up[v][j])
u = up[u][j], v = up[v][j];
return up[u][0];
}
int dis(int u, int v) {
return h[u] + h[v] - 2 * h[lca(u, v)];
}
void update(int u) {
for (int v = u; v != 0; v = par[v])
mn[v] = min(mn[v], dis(v, u));
}
bool ok(int u) {
for (int v = u; v != 0; v = par[v]) {
if (mn[v] + dis(u, v) < d) return 0;
}
return 1;
}
struct Centroid_Decomposition {
int sz[maxn];
int dfs(int u) {
sz[u] = 1;
for (int v : adj[u])
if (v != up[u][0] && !used[v])
sz[u] += dfs(v);
return sz[u];
}
int Centroid(int u, int tot) {
for (int v : adj[u])
if (v != up[u][0] && !used[v] && sz[v] > tot / 2)
return Centroid(v, tot);
return u;
}
void build(int u, int p) {
int c = Centroid(u, dfs(u));
used[c] = 1;
par[c] = p;
for (int v : adj[c])
if (!used[v])
build(v, c);
}
} Cen;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> d;
for (int u = 2; u <= n; u++) {
int v; cin >> v; v++;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 0);
Cen.build(1, 0);
for (int u = 1; u <= n; u++)
nodes.pb(u);
sort(nodes.begin(), nodes.end(),[&](int u, int v) {
return h[u] > h[v];
});
int res = 0;
memset(mn, 63, sizeof mn);
for (int u : nodes) {
if (ok(u)) {
res++;
update(u);
}
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5772 KB |
Output is correct |
4 |
Correct |
3 ms |
5800 KB |
Output is correct |
5 |
Correct |
4 ms |
5716 KB |
Output is correct |
6 |
Correct |
4 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5804 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
4 ms |
5716 KB |
Output is correct |
11 |
Correct |
3 ms |
5804 KB |
Output is correct |
12 |
Correct |
4 ms |
5800 KB |
Output is correct |
13 |
Correct |
4 ms |
5780 KB |
Output is correct |
14 |
Correct |
5 ms |
5828 KB |
Output is correct |
15 |
Correct |
5 ms |
5808 KB |
Output is correct |
16 |
Correct |
4 ms |
5716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
4 ms |
5808 KB |
Output is correct |
20 |
Correct |
3 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5772 KB |
Output is correct |
4 |
Correct |
3 ms |
5800 KB |
Output is correct |
5 |
Correct |
4 ms |
5716 KB |
Output is correct |
6 |
Correct |
4 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5804 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
4 ms |
5716 KB |
Output is correct |
11 |
Correct |
3 ms |
5804 KB |
Output is correct |
12 |
Correct |
4 ms |
5800 KB |
Output is correct |
13 |
Correct |
4 ms |
5780 KB |
Output is correct |
14 |
Correct |
5 ms |
5828 KB |
Output is correct |
15 |
Correct |
5 ms |
5808 KB |
Output is correct |
16 |
Correct |
4 ms |
5716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
4 ms |
5808 KB |
Output is correct |
20 |
Correct |
3 ms |
5716 KB |
Output is correct |
21 |
Correct |
15 ms |
6064 KB |
Output is correct |
22 |
Correct |
3 ms |
5844 KB |
Output is correct |
23 |
Correct |
3 ms |
5844 KB |
Output is correct |
24 |
Correct |
5 ms |
5844 KB |
Output is correct |
25 |
Correct |
4 ms |
5844 KB |
Output is correct |
26 |
Correct |
4 ms |
5844 KB |
Output is correct |
27 |
Correct |
4 ms |
5844 KB |
Output is correct |
28 |
Correct |
4 ms |
5932 KB |
Output is correct |
29 |
Correct |
4 ms |
5972 KB |
Output is correct |
30 |
Correct |
4 ms |
5972 KB |
Output is correct |
31 |
Correct |
4 ms |
6032 KB |
Output is correct |
32 |
Correct |
4 ms |
5932 KB |
Output is correct |
33 |
Correct |
4 ms |
5932 KB |
Output is correct |
34 |
Correct |
4 ms |
5972 KB |
Output is correct |
35 |
Correct |
3 ms |
5972 KB |
Output is correct |
36 |
Correct |
4 ms |
5972 KB |
Output is correct |
37 |
Correct |
4 ms |
5972 KB |
Output is correct |
38 |
Correct |
7 ms |
5972 KB |
Output is correct |
39 |
Correct |
8 ms |
6060 KB |
Output is correct |
40 |
Correct |
13 ms |
6160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5772 KB |
Output is correct |
4 |
Correct |
3 ms |
5800 KB |
Output is correct |
5 |
Correct |
4 ms |
5716 KB |
Output is correct |
6 |
Correct |
4 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5804 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
4 ms |
5716 KB |
Output is correct |
11 |
Correct |
3 ms |
5804 KB |
Output is correct |
12 |
Correct |
4 ms |
5800 KB |
Output is correct |
13 |
Correct |
4 ms |
5780 KB |
Output is correct |
14 |
Correct |
5 ms |
5828 KB |
Output is correct |
15 |
Correct |
5 ms |
5808 KB |
Output is correct |
16 |
Correct |
4 ms |
5716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
4 ms |
5808 KB |
Output is correct |
20 |
Correct |
3 ms |
5716 KB |
Output is correct |
21 |
Correct |
15 ms |
6064 KB |
Output is correct |
22 |
Correct |
3 ms |
5844 KB |
Output is correct |
23 |
Correct |
3 ms |
5844 KB |
Output is correct |
24 |
Correct |
5 ms |
5844 KB |
Output is correct |
25 |
Correct |
4 ms |
5844 KB |
Output is correct |
26 |
Correct |
4 ms |
5844 KB |
Output is correct |
27 |
Correct |
4 ms |
5844 KB |
Output is correct |
28 |
Correct |
4 ms |
5932 KB |
Output is correct |
29 |
Correct |
4 ms |
5972 KB |
Output is correct |
30 |
Correct |
4 ms |
5972 KB |
Output is correct |
31 |
Correct |
4 ms |
6032 KB |
Output is correct |
32 |
Correct |
4 ms |
5932 KB |
Output is correct |
33 |
Correct |
4 ms |
5932 KB |
Output is correct |
34 |
Correct |
4 ms |
5972 KB |
Output is correct |
35 |
Correct |
3 ms |
5972 KB |
Output is correct |
36 |
Correct |
4 ms |
5972 KB |
Output is correct |
37 |
Correct |
4 ms |
5972 KB |
Output is correct |
38 |
Correct |
7 ms |
5972 KB |
Output is correct |
39 |
Correct |
8 ms |
6060 KB |
Output is correct |
40 |
Correct |
13 ms |
6160 KB |
Output is correct |
41 |
Correct |
135 ms |
31212 KB |
Output is correct |
42 |
Correct |
251 ms |
19208 KB |
Output is correct |
43 |
Correct |
156 ms |
19204 KB |
Output is correct |
44 |
Correct |
192 ms |
19172 KB |
Output is correct |
45 |
Correct |
161 ms |
19140 KB |
Output is correct |
46 |
Correct |
452 ms |
32628 KB |
Output is correct |
47 |
Correct |
367 ms |
32508 KB |
Output is correct |
48 |
Correct |
355 ms |
32544 KB |
Output is correct |
49 |
Correct |
403 ms |
32500 KB |
Output is correct |
50 |
Correct |
75 ms |
19400 KB |
Output is correct |
51 |
Correct |
77 ms |
19464 KB |
Output is correct |
52 |
Correct |
84 ms |
19472 KB |
Output is correct |
53 |
Correct |
181 ms |
32928 KB |
Output is correct |
54 |
Correct |
190 ms |
32972 KB |
Output is correct |
55 |
Correct |
201 ms |
32980 KB |
Output is correct |
56 |
Correct |
20 ms |
6196 KB |
Output is correct |
57 |
Correct |
863 ms |
10464 KB |
Output is correct |
58 |
Execution timed out |
1085 ms |
26816 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |