#include <queue>
#include <vector>
#include <iostream>
#include <functional>
using namespace std;
int solve(int N, int K, vector<int> S, vector<int> V) {
int bits = 1;
while ((1 << bits) <= N) ++bits;
vector<vector<int> > P(bits, vector<int>(N));
vector<int> depth(N);
vector<vector<int> > G(N);
for (int i = 1; i < N; ++i) {
P[0][i] = S[i - 1];
depth[i] = depth[P[0][i]] + 1;
G[P[0][i]].push_back(i);
}
vector<bool> coin(N);
for (int i = 0; i < K; ++i) {
coin[V[i]] = true;
}
for (int i = 1; i < bits; ++i) {
for (int j = 0; j < N; ++j) {
P[i][j] = P[i - 1][P[i - 1][j]];
}
}
int cnt = 0;
vector<int> sl(N), sr(N), ord;
function<void(int)> eulertour = [&](int pos) {
ord.push_back(pos);
sl[pos] = cnt++;
for (int i : G[pos]) {
eulertour(i);
}
sr[pos] = cnt;
};
function<int(int, int)> ancestor = [&](int pos, int level) {
for (int i = bits - 1; i >= 0; --i) {
if (level >= (1 << i)) {
pos = P[i][pos];
level -= (1 << i);
}
}
return pos;
};
eulertour(0);
if (coin[0]) return 1;
int L = 0, R = N + 1;
vector<vector<int> > query(N);
for (int i = 1; i < N; ++i) {
if (!coin[i]) continue;
int pos = ancestor(i, (depth[i] - 1) / 2);
query[sl[pos]].push_back(sr[pos]);
}
while (R - L > 1) {
int M = (L + R) >> 1;
priority_queue<int, vector<int>, greater<int> > que;
bool ok = true;
for (int i = 0; i < N && ok; ++i) {
for (int j : query[i]) {
que.push(j);
}
if (depth[ord[i]] >= M && !que.empty()) {
int u = que.top(); que.pop();
if (u <= i) ok = false;
}
}
if (!que.empty()) ok = false;
if (ok) L = M;
else R = M;
}
return R;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vector<int> S(N - 1);
for (int i = 0; i < N - 1; ++i) {
cin >> S[i]; --S[i];
}
vector<int> V(K);
for (int i = 0; i < K; ++i) {
cin >> V[i]; --V[i];
}
int ans = solve(N, K, S, V);
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
1536 KB |
Output is correct |
4 |
Correct |
4 ms |
1536 KB |
Output is correct |
5 |
Correct |
3 ms |
1408 KB |
Output is correct |
6 |
Correct |
77 ms |
46696 KB |
Output is correct |
7 |
Correct |
75 ms |
46568 KB |
Output is correct |
8 |
Correct |
2 ms |
1408 KB |
Output is correct |
9 |
Correct |
188 ms |
52072 KB |
Output is correct |
10 |
Correct |
218 ms |
53352 KB |
Output is correct |
11 |
Correct |
147 ms |
49384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
1152 KB |
Output is correct |
3 |
Correct |
2 ms |
1152 KB |
Output is correct |
4 |
Correct |
2 ms |
1024 KB |
Output is correct |
5 |
Correct |
2 ms |
1152 KB |
Output is correct |
6 |
Correct |
2 ms |
1024 KB |
Output is correct |
7 |
Correct |
77 ms |
46696 KB |
Output is correct |
8 |
Correct |
75 ms |
46568 KB |
Output is correct |
9 |
Correct |
2 ms |
1408 KB |
Output is correct |
10 |
Correct |
67 ms |
38760 KB |
Output is correct |
11 |
Correct |
67 ms |
38760 KB |
Output is correct |
12 |
Correct |
85 ms |
33000 KB |
Output is correct |
13 |
Correct |
77 ms |
40040 KB |
Output is correct |
14 |
Correct |
71 ms |
33896 KB |
Output is correct |
15 |
Correct |
82 ms |
33004 KB |
Output is correct |
16 |
Correct |
75 ms |
40168 KB |
Output is correct |
17 |
Correct |
102 ms |
34024 KB |
Output is correct |
18 |
Correct |
74 ms |
42856 KB |
Output is correct |
19 |
Correct |
69 ms |
37740 KB |
Output is correct |
20 |
Correct |
69 ms |
37352 KB |
Output is correct |
21 |
Correct |
78 ms |
37428 KB |
Output is correct |
22 |
Correct |
70 ms |
37352 KB |
Output is correct |
23 |
Correct |
71 ms |
36884 KB |
Output is correct |
24 |
Correct |
71 ms |
37096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
1152 KB |
Output is correct |
7 |
Correct |
2 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1280 KB |
Output is correct |
9 |
Correct |
2 ms |
1152 KB |
Output is correct |
10 |
Correct |
2 ms |
1024 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
3 ms |
1152 KB |
Output is correct |
13 |
Correct |
3 ms |
1280 KB |
Output is correct |
14 |
Correct |
3 ms |
1152 KB |
Output is correct |
15 |
Correct |
3 ms |
1280 KB |
Output is correct |
16 |
Correct |
3 ms |
1280 KB |
Output is correct |
17 |
Correct |
2 ms |
1152 KB |
Output is correct |
18 |
Correct |
4 ms |
1408 KB |
Output is correct |
19 |
Correct |
2 ms |
1280 KB |
Output is correct |
20 |
Correct |
3 ms |
1024 KB |
Output is correct |
21 |
Correct |
2 ms |
896 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
1536 KB |
Output is correct |
25 |
Correct |
4 ms |
1536 KB |
Output is correct |
26 |
Correct |
3 ms |
1408 KB |
Output is correct |
27 |
Correct |
0 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
1152 KB |
Output is correct |
29 |
Correct |
2 ms |
1152 KB |
Output is correct |
30 |
Correct |
2 ms |
1024 KB |
Output is correct |
31 |
Correct |
2 ms |
1152 KB |
Output is correct |
32 |
Correct |
2 ms |
1024 KB |
Output is correct |
33 |
Correct |
2 ms |
1408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
1152 KB |
Output is correct |
7 |
Correct |
2 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1280 KB |
Output is correct |
9 |
Correct |
2 ms |
1152 KB |
Output is correct |
10 |
Correct |
2 ms |
1024 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
3 ms |
1152 KB |
Output is correct |
13 |
Correct |
3 ms |
1280 KB |
Output is correct |
14 |
Correct |
3 ms |
1152 KB |
Output is correct |
15 |
Correct |
3 ms |
1280 KB |
Output is correct |
16 |
Correct |
3 ms |
1280 KB |
Output is correct |
17 |
Correct |
2 ms |
1152 KB |
Output is correct |
18 |
Correct |
4 ms |
1408 KB |
Output is correct |
19 |
Correct |
2 ms |
1280 KB |
Output is correct |
20 |
Correct |
3 ms |
1024 KB |
Output is correct |
21 |
Correct |
2 ms |
896 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
1536 KB |
Output is correct |
25 |
Correct |
4 ms |
1536 KB |
Output is correct |
26 |
Correct |
3 ms |
1408 KB |
Output is correct |
27 |
Correct |
0 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
1152 KB |
Output is correct |
29 |
Correct |
2 ms |
1152 KB |
Output is correct |
30 |
Correct |
2 ms |
1024 KB |
Output is correct |
31 |
Correct |
2 ms |
1152 KB |
Output is correct |
32 |
Correct |
2 ms |
1024 KB |
Output is correct |
33 |
Correct |
77 ms |
46696 KB |
Output is correct |
34 |
Correct |
75 ms |
46568 KB |
Output is correct |
35 |
Correct |
2 ms |
1408 KB |
Output is correct |
36 |
Correct |
67 ms |
38760 KB |
Output is correct |
37 |
Correct |
67 ms |
38760 KB |
Output is correct |
38 |
Correct |
85 ms |
33000 KB |
Output is correct |
39 |
Correct |
77 ms |
40040 KB |
Output is correct |
40 |
Correct |
71 ms |
33896 KB |
Output is correct |
41 |
Correct |
82 ms |
33004 KB |
Output is correct |
42 |
Correct |
75 ms |
40168 KB |
Output is correct |
43 |
Correct |
102 ms |
34024 KB |
Output is correct |
44 |
Correct |
74 ms |
42856 KB |
Output is correct |
45 |
Correct |
69 ms |
37740 KB |
Output is correct |
46 |
Correct |
69 ms |
37352 KB |
Output is correct |
47 |
Correct |
78 ms |
37428 KB |
Output is correct |
48 |
Correct |
70 ms |
37352 KB |
Output is correct |
49 |
Correct |
71 ms |
36884 KB |
Output is correct |
50 |
Correct |
71 ms |
37096 KB |
Output is correct |
51 |
Correct |
188 ms |
52072 KB |
Output is correct |
52 |
Correct |
218 ms |
53352 KB |
Output is correct |
53 |
Correct |
147 ms |
49384 KB |
Output is correct |
54 |
Correct |
84 ms |
39400 KB |
Output is correct |
55 |
Correct |
92 ms |
39420 KB |
Output is correct |
56 |
Correct |
142 ms |
41832 KB |
Output is correct |
57 |
Correct |
106 ms |
35688 KB |
Output is correct |
58 |
Correct |
83 ms |
32872 KB |
Output is correct |
59 |
Correct |
239 ms |
39016 KB |
Output is correct |
60 |
Correct |
136 ms |
35176 KB |
Output is correct |
61 |
Correct |
154 ms |
44868 KB |
Output is correct |
62 |
Correct |
150 ms |
35688 KB |
Output is correct |
63 |
Correct |
120 ms |
44648 KB |
Output is correct |
64 |
Correct |
106 ms |
38632 KB |
Output is correct |
65 |
Correct |
94 ms |
37992 KB |
Output is correct |
66 |
Correct |
95 ms |
39528 KB |
Output is correct |
67 |
Correct |
115 ms |
39400 KB |
Output is correct |
68 |
Correct |
111 ms |
38888 KB |
Output is correct |
69 |
Correct |
139 ms |
41796 KB |
Output is correct |
70 |
Correct |
69 ms |
19432 KB |
Output is correct |
71 |
Correct |
121 ms |
38192 KB |
Output is correct |