#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int ans;
const int MAXN = 5e5+5;
int sl[MAXN];
int depth[MAXN];
vector<int> ord;
int sr[MAXN];
int p[25][MAXN];
vector<int> v1[MAXN];
int arr[MAXN];
bool coin[MAXN];
int timer;
vector<int> query[MAXN];
void eulertour(int curr){
ord.push_back(curr);
sl[curr] = timer++;
for(int x:v1[curr]){
eulertour(x);
}
sr[curr] = timer;
}
int ancestor(int curr,int level){
for(int i=20;i>=0;i--){
if(level-(1<<i)>=0){
level-=(1<<i);
curr = p[i][curr];
}
}
return curr;
}
int main(){
int n,k;
cin>>n>>k;
for(int i=1;i<n;i++){
cin>>arr[i];
}
for(int i=1;i<=k;i++){
int x;
cin>>x;
coin[x] = true;
}
for(int i=2;i<=n;i++){
p[0][i] = arr[i-1];
depth[i] = depth[p[0][i]]+1;
v1[p[0][i]].push_back(i);
}
for(int i=1;i<=20;i++){
for(int j=1;j<=n;j++){
p[i][j] = p[i-1][p[i-1][j]];
}
}
eulertour(1);
if(coin[1]){
cout<<1<<endl;
return 0;
}
int l =0;
int r = n;
for(int i=2;i<=n;i++){
if(!coin[i]){
continue;
}
int pos = ancestor(i,(depth[i]-1)/2);
query[sl[pos]].push_back(sr[pos]);
}
while(l<=r){
int mid = (l+r)/2;
priority_queue<int,vector<int>, greater<int> > pq;
bool ok = true;
for(int i=0;i<n && ok;i++){
for(int j:query[i]){
pq.push(j);
//timer value(sorts by latest node in the subtree)
}
if(depth[ord[i]]>=mid && !pq.empty()){
int u = pq.top();
pq.pop();
if(u<=i){
ok = false;
}
}
}
if(!pq.empty()){
ok = false;
}
if(ok){
l = mid+1;
ans = mid;
}else{
r = mid-1;
}
}
cout<<ans+1<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
24056 KB |
Output is correct |
2 |
Correct |
23 ms |
24028 KB |
Output is correct |
3 |
Correct |
29 ms |
25080 KB |
Output is correct |
4 |
Correct |
30 ms |
25080 KB |
Output is correct |
5 |
Correct |
29 ms |
24924 KB |
Output is correct |
6 |
Correct |
179 ms |
61296 KB |
Output is correct |
7 |
Correct |
183 ms |
61420 KB |
Output is correct |
8 |
Correct |
27 ms |
24924 KB |
Output is correct |
9 |
Correct |
396 ms |
66768 KB |
Output is correct |
10 |
Correct |
428 ms |
67408 KB |
Output is correct |
11 |
Correct |
312 ms |
64524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
24056 KB |
Output is correct |
2 |
Correct |
26 ms |
24824 KB |
Output is correct |
3 |
Correct |
26 ms |
24824 KB |
Output is correct |
4 |
Correct |
26 ms |
24568 KB |
Output is correct |
5 |
Correct |
27 ms |
24684 KB |
Output is correct |
6 |
Correct |
26 ms |
24696 KB |
Output is correct |
7 |
Correct |
179 ms |
61296 KB |
Output is correct |
8 |
Correct |
183 ms |
61420 KB |
Output is correct |
9 |
Correct |
27 ms |
24924 KB |
Output is correct |
10 |
Correct |
168 ms |
53644 KB |
Output is correct |
11 |
Correct |
167 ms |
53664 KB |
Output is correct |
12 |
Correct |
164 ms |
48104 KB |
Output is correct |
13 |
Correct |
177 ms |
54764 KB |
Output is correct |
14 |
Correct |
164 ms |
48792 KB |
Output is correct |
15 |
Correct |
168 ms |
48348 KB |
Output is correct |
16 |
Correct |
181 ms |
54860 KB |
Output is correct |
17 |
Correct |
211 ms |
49092 KB |
Output is correct |
18 |
Correct |
175 ms |
57632 KB |
Output is correct |
19 |
Correct |
184 ms |
52752 KB |
Output is correct |
20 |
Correct |
222 ms |
52316 KB |
Output is correct |
21 |
Correct |
209 ms |
52360 KB |
Output is correct |
22 |
Correct |
225 ms |
52460 KB |
Output is correct |
23 |
Correct |
219 ms |
51692 KB |
Output is correct |
24 |
Correct |
194 ms |
52232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
24056 KB |
Output is correct |
2 |
Correct |
23 ms |
24056 KB |
Output is correct |
3 |
Correct |
27 ms |
23928 KB |
Output is correct |
4 |
Correct |
24 ms |
24056 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
27 ms |
24696 KB |
Output is correct |
7 |
Correct |
28 ms |
24824 KB |
Output is correct |
8 |
Correct |
33 ms |
24824 KB |
Output is correct |
9 |
Correct |
28 ms |
24696 KB |
Output is correct |
10 |
Correct |
26 ms |
24568 KB |
Output is correct |
11 |
Correct |
26 ms |
24824 KB |
Output is correct |
12 |
Correct |
28 ms |
24696 KB |
Output is correct |
13 |
Correct |
30 ms |
24824 KB |
Output is correct |
14 |
Correct |
28 ms |
24824 KB |
Output is correct |
15 |
Correct |
28 ms |
24952 KB |
Output is correct |
16 |
Correct |
28 ms |
24952 KB |
Output is correct |
17 |
Correct |
27 ms |
24696 KB |
Output is correct |
18 |
Correct |
31 ms |
24952 KB |
Output is correct |
19 |
Correct |
28 ms |
24824 KB |
Output is correct |
20 |
Correct |
27 ms |
24568 KB |
Output is correct |
21 |
Correct |
26 ms |
24540 KB |
Output is correct |
22 |
Correct |
23 ms |
24056 KB |
Output is correct |
23 |
Correct |
23 ms |
24028 KB |
Output is correct |
24 |
Correct |
29 ms |
25080 KB |
Output is correct |
25 |
Correct |
30 ms |
25080 KB |
Output is correct |
26 |
Correct |
29 ms |
24924 KB |
Output is correct |
27 |
Correct |
28 ms |
24056 KB |
Output is correct |
28 |
Correct |
26 ms |
24824 KB |
Output is correct |
29 |
Correct |
26 ms |
24824 KB |
Output is correct |
30 |
Correct |
26 ms |
24568 KB |
Output is correct |
31 |
Correct |
27 ms |
24684 KB |
Output is correct |
32 |
Correct |
26 ms |
24696 KB |
Output is correct |
33 |
Correct |
27 ms |
24924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
24056 KB |
Output is correct |
2 |
Correct |
23 ms |
24056 KB |
Output is correct |
3 |
Correct |
27 ms |
23928 KB |
Output is correct |
4 |
Correct |
24 ms |
24056 KB |
Output is correct |
5 |
Correct |
23 ms |
23928 KB |
Output is correct |
6 |
Correct |
27 ms |
24696 KB |
Output is correct |
7 |
Correct |
28 ms |
24824 KB |
Output is correct |
8 |
Correct |
33 ms |
24824 KB |
Output is correct |
9 |
Correct |
28 ms |
24696 KB |
Output is correct |
10 |
Correct |
26 ms |
24568 KB |
Output is correct |
11 |
Correct |
26 ms |
24824 KB |
Output is correct |
12 |
Correct |
28 ms |
24696 KB |
Output is correct |
13 |
Correct |
30 ms |
24824 KB |
Output is correct |
14 |
Correct |
28 ms |
24824 KB |
Output is correct |
15 |
Correct |
28 ms |
24952 KB |
Output is correct |
16 |
Correct |
28 ms |
24952 KB |
Output is correct |
17 |
Correct |
27 ms |
24696 KB |
Output is correct |
18 |
Correct |
31 ms |
24952 KB |
Output is correct |
19 |
Correct |
28 ms |
24824 KB |
Output is correct |
20 |
Correct |
27 ms |
24568 KB |
Output is correct |
21 |
Correct |
26 ms |
24540 KB |
Output is correct |
22 |
Correct |
23 ms |
24056 KB |
Output is correct |
23 |
Correct |
23 ms |
24028 KB |
Output is correct |
24 |
Correct |
29 ms |
25080 KB |
Output is correct |
25 |
Correct |
30 ms |
25080 KB |
Output is correct |
26 |
Correct |
29 ms |
24924 KB |
Output is correct |
27 |
Correct |
28 ms |
24056 KB |
Output is correct |
28 |
Correct |
26 ms |
24824 KB |
Output is correct |
29 |
Correct |
26 ms |
24824 KB |
Output is correct |
30 |
Correct |
26 ms |
24568 KB |
Output is correct |
31 |
Correct |
27 ms |
24684 KB |
Output is correct |
32 |
Correct |
26 ms |
24696 KB |
Output is correct |
33 |
Correct |
179 ms |
61296 KB |
Output is correct |
34 |
Correct |
183 ms |
61420 KB |
Output is correct |
35 |
Correct |
27 ms |
24924 KB |
Output is correct |
36 |
Correct |
168 ms |
53644 KB |
Output is correct |
37 |
Correct |
167 ms |
53664 KB |
Output is correct |
38 |
Correct |
164 ms |
48104 KB |
Output is correct |
39 |
Correct |
177 ms |
54764 KB |
Output is correct |
40 |
Correct |
164 ms |
48792 KB |
Output is correct |
41 |
Correct |
168 ms |
48348 KB |
Output is correct |
42 |
Correct |
181 ms |
54860 KB |
Output is correct |
43 |
Correct |
211 ms |
49092 KB |
Output is correct |
44 |
Correct |
175 ms |
57632 KB |
Output is correct |
45 |
Correct |
184 ms |
52752 KB |
Output is correct |
46 |
Correct |
222 ms |
52316 KB |
Output is correct |
47 |
Correct |
209 ms |
52360 KB |
Output is correct |
48 |
Correct |
225 ms |
52460 KB |
Output is correct |
49 |
Correct |
219 ms |
51692 KB |
Output is correct |
50 |
Correct |
194 ms |
52232 KB |
Output is correct |
51 |
Correct |
396 ms |
66768 KB |
Output is correct |
52 |
Correct |
428 ms |
67408 KB |
Output is correct |
53 |
Correct |
312 ms |
64524 KB |
Output is correct |
54 |
Correct |
214 ms |
54452 KB |
Output is correct |
55 |
Correct |
209 ms |
54380 KB |
Output is correct |
56 |
Correct |
291 ms |
57056 KB |
Output is correct |
57 |
Correct |
257 ms |
50880 KB |
Output is correct |
58 |
Correct |
165 ms |
48108 KB |
Output is correct |
59 |
Correct |
417 ms |
54284 KB |
Output is correct |
60 |
Correct |
244 ms |
50668 KB |
Output is correct |
61 |
Correct |
318 ms |
59352 KB |
Output is correct |
62 |
Correct |
293 ms |
51176 KB |
Output is correct |
63 |
Correct |
275 ms |
60080 KB |
Output is correct |
64 |
Correct |
275 ms |
54124 KB |
Output is correct |
65 |
Correct |
236 ms |
53480 KB |
Output is correct |
66 |
Correct |
278 ms |
54880 KB |
Output is correct |
67 |
Correct |
282 ms |
54652 KB |
Output is correct |
68 |
Correct |
273 ms |
53884 KB |
Output is correct |
69 |
Correct |
332 ms |
56576 KB |
Output is correct |
70 |
Correct |
170 ms |
38820 KB |
Output is correct |
71 |
Correct |
266 ms |
54328 KB |
Output is correct |