#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> adj[200005];
priority_queue<int, vector<int>> pq[200005];
int need[200005], can[200005], depth[200005];
bitset<200005> coin;
void DFS(int u, int p) {
depth[u] = depth[p] + 1;
for(int v : adj[u]) {
if(v == p) continue;
DFS(v, u);
if(pq[v].size() > pq[u].size()) swap(pq[u], pq[v]);
need[u] += need[v];
}
for(int v : adj[u]) {
if(v == p) continue;
while(!pq[v].empty()) pq[u].emplace(pq[v].top()), pq[v].pop();
}
if(coin[u]) pq[u].emplace(depth[u] / 2 + (depth[u] & 1));
while(!pq[u].empty() && pq[u].top() == depth[u]) {
need[u]++;
pq[u].pop();
}
// cout << u << " " << need[u] << "\n";
}
int l, r, mid, ans;
bool ok;
void check(int u, int p) {
if(depth[u] >= mid) can[u] = 1;
else can[u] = 0;
for(int v : adj[u]) {
if(v == p) continue;
check(v, u);
can[u] += can[v];
}
if(can[u] < need[u]) ok = 0;
}
int main() {
depth[0] = -2;
ios_base::sync_with_stdio(0);
cin >> N >> M;
int u;
for(int i = 2; i <= N; i++) {
cin >> u;
adj[u].push_back(i);
adj[i].push_back(u);
}
for(int i = 1; i <= M; i++) {
cin >> u;
coin[u] = 1;
}
if(coin[1] == 1) {
cout << "1\n";
exit(0);
}
DFS(1, 0);
l = -1, r = N - 2;
while(l <= r) {
mid = (l + r) >> 1;
ok = 1;
check(1, 0);
if(ok) {
ans = mid;
l = mid + 1;
}else r = mid - 1;
}
cout << ans + 2 << "\n";
cin >> N;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11392 KB |
Output is correct |
2 |
Correct |
9 ms |
11264 KB |
Output is correct |
3 |
Correct |
10 ms |
12032 KB |
Output is correct |
4 |
Correct |
12 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12032 KB |
Output is correct |
6 |
Correct |
155 ms |
41848 KB |
Output is correct |
7 |
Correct |
161 ms |
41848 KB |
Output is correct |
8 |
Correct |
9 ms |
12032 KB |
Output is correct |
9 |
Correct |
214 ms |
42324 KB |
Output is correct |
10 |
Correct |
201 ms |
42348 KB |
Output is correct |
11 |
Correct |
177 ms |
42100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11264 KB |
Output is correct |
2 |
Correct |
8 ms |
11776 KB |
Output is correct |
3 |
Correct |
8 ms |
11776 KB |
Output is correct |
4 |
Correct |
11 ms |
11520 KB |
Output is correct |
5 |
Correct |
9 ms |
11776 KB |
Output is correct |
6 |
Correct |
9 ms |
11520 KB |
Output is correct |
7 |
Correct |
155 ms |
41848 KB |
Output is correct |
8 |
Correct |
161 ms |
41848 KB |
Output is correct |
9 |
Correct |
9 ms |
12032 KB |
Output is correct |
10 |
Correct |
113 ms |
28656 KB |
Output is correct |
11 |
Correct |
137 ms |
28656 KB |
Output is correct |
12 |
Correct |
398 ms |
20456 KB |
Output is correct |
13 |
Correct |
174 ms |
30840 KB |
Output is correct |
14 |
Correct |
101 ms |
19576 KB |
Output is correct |
15 |
Correct |
386 ms |
20588 KB |
Output is correct |
16 |
Correct |
173 ms |
30840 KB |
Output is correct |
17 |
Correct |
565 ms |
20216 KB |
Output is correct |
18 |
Correct |
154 ms |
33016 KB |
Output is correct |
19 |
Correct |
165 ms |
21240 KB |
Output is correct |
20 |
Correct |
183 ms |
20216 KB |
Output is correct |
21 |
Correct |
180 ms |
20280 KB |
Output is correct |
22 |
Correct |
183 ms |
20216 KB |
Output is correct |
23 |
Correct |
155 ms |
19976 KB |
Output is correct |
24 |
Correct |
230 ms |
20088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11352 KB |
Output is correct |
2 |
Correct |
7 ms |
11392 KB |
Output is correct |
3 |
Correct |
7 ms |
11264 KB |
Output is correct |
4 |
Correct |
9 ms |
11264 KB |
Output is correct |
5 |
Correct |
7 ms |
11264 KB |
Output is correct |
6 |
Correct |
10 ms |
11776 KB |
Output is correct |
7 |
Correct |
11 ms |
11776 KB |
Output is correct |
8 |
Correct |
9 ms |
11776 KB |
Output is correct |
9 |
Correct |
9 ms |
11520 KB |
Output is correct |
10 |
Correct |
9 ms |
11520 KB |
Output is correct |
11 |
Correct |
9 ms |
11776 KB |
Output is correct |
12 |
Correct |
10 ms |
11648 KB |
Output is correct |
13 |
Correct |
10 ms |
11776 KB |
Output is correct |
14 |
Correct |
10 ms |
11648 KB |
Output is correct |
15 |
Correct |
13 ms |
11904 KB |
Output is correct |
16 |
Correct |
9 ms |
11776 KB |
Output is correct |
17 |
Correct |
9 ms |
11520 KB |
Output is correct |
18 |
Correct |
9 ms |
11776 KB |
Output is correct |
19 |
Correct |
11 ms |
11520 KB |
Output is correct |
20 |
Correct |
9 ms |
11648 KB |
Output is correct |
21 |
Correct |
10 ms |
11648 KB |
Output is correct |
22 |
Correct |
7 ms |
11392 KB |
Output is correct |
23 |
Correct |
9 ms |
11264 KB |
Output is correct |
24 |
Correct |
10 ms |
12032 KB |
Output is correct |
25 |
Correct |
12 ms |
12160 KB |
Output is correct |
26 |
Correct |
9 ms |
12032 KB |
Output is correct |
27 |
Correct |
7 ms |
11264 KB |
Output is correct |
28 |
Correct |
8 ms |
11776 KB |
Output is correct |
29 |
Correct |
8 ms |
11776 KB |
Output is correct |
30 |
Correct |
11 ms |
11520 KB |
Output is correct |
31 |
Correct |
9 ms |
11776 KB |
Output is correct |
32 |
Correct |
9 ms |
11520 KB |
Output is correct |
33 |
Correct |
9 ms |
12032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11352 KB |
Output is correct |
2 |
Correct |
7 ms |
11392 KB |
Output is correct |
3 |
Correct |
7 ms |
11264 KB |
Output is correct |
4 |
Correct |
9 ms |
11264 KB |
Output is correct |
5 |
Correct |
7 ms |
11264 KB |
Output is correct |
6 |
Correct |
10 ms |
11776 KB |
Output is correct |
7 |
Correct |
11 ms |
11776 KB |
Output is correct |
8 |
Correct |
9 ms |
11776 KB |
Output is correct |
9 |
Correct |
9 ms |
11520 KB |
Output is correct |
10 |
Correct |
9 ms |
11520 KB |
Output is correct |
11 |
Correct |
9 ms |
11776 KB |
Output is correct |
12 |
Correct |
10 ms |
11648 KB |
Output is correct |
13 |
Correct |
10 ms |
11776 KB |
Output is correct |
14 |
Correct |
10 ms |
11648 KB |
Output is correct |
15 |
Correct |
13 ms |
11904 KB |
Output is correct |
16 |
Correct |
9 ms |
11776 KB |
Output is correct |
17 |
Correct |
9 ms |
11520 KB |
Output is correct |
18 |
Correct |
9 ms |
11776 KB |
Output is correct |
19 |
Correct |
11 ms |
11520 KB |
Output is correct |
20 |
Correct |
9 ms |
11648 KB |
Output is correct |
21 |
Correct |
10 ms |
11648 KB |
Output is correct |
22 |
Correct |
7 ms |
11392 KB |
Output is correct |
23 |
Correct |
9 ms |
11264 KB |
Output is correct |
24 |
Correct |
10 ms |
12032 KB |
Output is correct |
25 |
Correct |
12 ms |
12160 KB |
Output is correct |
26 |
Correct |
9 ms |
12032 KB |
Output is correct |
27 |
Correct |
7 ms |
11264 KB |
Output is correct |
28 |
Correct |
8 ms |
11776 KB |
Output is correct |
29 |
Correct |
8 ms |
11776 KB |
Output is correct |
30 |
Correct |
11 ms |
11520 KB |
Output is correct |
31 |
Correct |
9 ms |
11776 KB |
Output is correct |
32 |
Correct |
9 ms |
11520 KB |
Output is correct |
33 |
Correct |
155 ms |
41848 KB |
Output is correct |
34 |
Correct |
161 ms |
41848 KB |
Output is correct |
35 |
Correct |
9 ms |
12032 KB |
Output is correct |
36 |
Correct |
113 ms |
28656 KB |
Output is correct |
37 |
Correct |
137 ms |
28656 KB |
Output is correct |
38 |
Correct |
398 ms |
20456 KB |
Output is correct |
39 |
Correct |
174 ms |
30840 KB |
Output is correct |
40 |
Correct |
101 ms |
19576 KB |
Output is correct |
41 |
Correct |
386 ms |
20588 KB |
Output is correct |
42 |
Correct |
173 ms |
30840 KB |
Output is correct |
43 |
Correct |
565 ms |
20216 KB |
Output is correct |
44 |
Correct |
154 ms |
33016 KB |
Output is correct |
45 |
Correct |
165 ms |
21240 KB |
Output is correct |
46 |
Correct |
183 ms |
20216 KB |
Output is correct |
47 |
Correct |
180 ms |
20280 KB |
Output is correct |
48 |
Correct |
183 ms |
20216 KB |
Output is correct |
49 |
Correct |
155 ms |
19976 KB |
Output is correct |
50 |
Correct |
230 ms |
20088 KB |
Output is correct |
51 |
Correct |
214 ms |
42324 KB |
Output is correct |
52 |
Correct |
201 ms |
42348 KB |
Output is correct |
53 |
Correct |
177 ms |
42100 KB |
Output is correct |
54 |
Correct |
127 ms |
30192 KB |
Output is correct |
55 |
Correct |
126 ms |
30192 KB |
Output is correct |
56 |
Correct |
140 ms |
31344 KB |
Output is correct |
57 |
Correct |
135 ms |
22264 KB |
Output is correct |
58 |
Correct |
334 ms |
20444 KB |
Output is correct |
59 |
Correct |
185 ms |
26224 KB |
Output is correct |
60 |
Correct |
407 ms |
22392 KB |
Output is correct |
61 |
Correct |
192 ms |
32880 KB |
Output is correct |
62 |
Correct |
583 ms |
22520 KB |
Output is correct |
63 |
Correct |
174 ms |
33396 KB |
Output is correct |
64 |
Correct |
175 ms |
21496 KB |
Output is correct |
65 |
Correct |
189 ms |
20600 KB |
Output is correct |
66 |
Correct |
197 ms |
20344 KB |
Output is correct |
67 |
Correct |
226 ms |
21000 KB |
Output is correct |
68 |
Correct |
184 ms |
21108 KB |
Output is correct |
69 |
Correct |
253 ms |
20476 KB |
Output is correct |
70 |
Correct |
134 ms |
20336 KB |
Output is correct |
71 |
Correct |
171 ms |
28280 KB |
Output is correct |