#include<bits/stdc++.h>
using namespace std;
int n, m, cnt, x;
int par[18][200005], dep[200005], lft[200005], rgt[200005];
bool b[200005];
vector<int> adjl[200005], que[200005], order;
void tour(int x){
order.push_back(x);
lft[x]=cnt++;
for(int i:adjl[x]){
tour(i);
}
rgt[x]=cnt;
}
int anc(int x, int dp){
for(int i=17; i>=0; i--){
if(dp&(1<<i)) x=par[i][x];
}
return x;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=2; i<=n; i++){
cin >> x;
par[0][i]=x;
dep[i]=dep[x]+1;
adjl[x].push_back(i);
}
for(int i=1; i<18; i++){
for(int j=1; j<=n; j++){
par[i][j]=par[i-1][par[i-1][j]];
}
}
for(int i=0; i<m; i++){
cin >> x;
b[x]=1;
}
tour(1);
for(int i=1; i<=n; i++){
if(b[i]){
int up=anc(i, (dep[i]-1)/2);
que[lft[up]].push_back(rgt[up]);
}
}
int res=0, mid, l=0, r=n;
while(l<=r){
mid=(l+r)/2;
priority_queue<int, vector<int>, greater<int>> pq;
bool can=1;
for(int i=0; i<n && can; i++){
for(int j:que[i]) pq.push(j);
if(dep[order[i]]>=mid && !pq.empty()){
int x=pq.top();
pq.pop();
if(x<=i) can=0;
}
}
if(!pq.empty()) can=0;
if(can){
res=mid;
l=mid+1;
}else{
r=mid-1;
}
}
cout << res+1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
10724 KB |
Output is correct |
4 |
Correct |
8 ms |
10756 KB |
Output is correct |
5 |
Correct |
6 ms |
10708 KB |
Output is correct |
6 |
Correct |
48 ms |
43864 KB |
Output is correct |
7 |
Correct |
47 ms |
43980 KB |
Output is correct |
8 |
Correct |
6 ms |
10708 KB |
Output is correct |
9 |
Correct |
126 ms |
49348 KB |
Output is correct |
10 |
Correct |
143 ms |
50088 KB |
Output is correct |
11 |
Correct |
93 ms |
47036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
43864 KB |
Output is correct |
2 |
Correct |
47 ms |
43980 KB |
Output is correct |
3 |
Correct |
6 ms |
10708 KB |
Output is correct |
4 |
Correct |
6 ms |
9772 KB |
Output is correct |
5 |
Correct |
7 ms |
10480 KB |
Output is correct |
6 |
Correct |
6 ms |
10400 KB |
Output is correct |
7 |
Correct |
6 ms |
10376 KB |
Output is correct |
8 |
Correct |
5 ms |
10508 KB |
Output is correct |
9 |
Correct |
6 ms |
10324 KB |
Output is correct |
10 |
Correct |
42 ms |
36076 KB |
Output is correct |
11 |
Correct |
50 ms |
36136 KB |
Output is correct |
12 |
Correct |
50 ms |
30540 KB |
Output is correct |
13 |
Correct |
47 ms |
37372 KB |
Output is correct |
14 |
Correct |
41 ms |
31356 KB |
Output is correct |
15 |
Correct |
49 ms |
30672 KB |
Output is correct |
16 |
Correct |
47 ms |
37312 KB |
Output is correct |
17 |
Correct |
63 ms |
31600 KB |
Output is correct |
18 |
Correct |
53 ms |
40332 KB |
Output is correct |
19 |
Correct |
48 ms |
35392 KB |
Output is correct |
20 |
Correct |
48 ms |
34936 KB |
Output is correct |
21 |
Correct |
49 ms |
34840 KB |
Output is correct |
22 |
Correct |
48 ms |
34972 KB |
Output is correct |
23 |
Correct |
50 ms |
34296 KB |
Output is correct |
24 |
Correct |
46 ms |
34764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
10724 KB |
Output is correct |
4 |
Correct |
8 ms |
10756 KB |
Output is correct |
5 |
Correct |
6 ms |
10708 KB |
Output is correct |
6 |
Correct |
6 ms |
9772 KB |
Output is correct |
7 |
Correct |
7 ms |
10480 KB |
Output is correct |
8 |
Correct |
6 ms |
10400 KB |
Output is correct |
9 |
Correct |
6 ms |
10376 KB |
Output is correct |
10 |
Correct |
5 ms |
10508 KB |
Output is correct |
11 |
Correct |
6 ms |
10324 KB |
Output is correct |
12 |
Correct |
6 ms |
10708 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
4 ms |
9812 KB |
Output is correct |
17 |
Correct |
4 ms |
9732 KB |
Output is correct |
18 |
Correct |
6 ms |
10508 KB |
Output is correct |
19 |
Correct |
5 ms |
10508 KB |
Output is correct |
20 |
Correct |
6 ms |
10452 KB |
Output is correct |
21 |
Correct |
6 ms |
10348 KB |
Output is correct |
22 |
Correct |
5 ms |
10324 KB |
Output is correct |
23 |
Correct |
6 ms |
10452 KB |
Output is correct |
24 |
Correct |
6 ms |
10440 KB |
Output is correct |
25 |
Correct |
6 ms |
10580 KB |
Output is correct |
26 |
Correct |
6 ms |
10324 KB |
Output is correct |
27 |
Correct |
6 ms |
10580 KB |
Output is correct |
28 |
Correct |
6 ms |
10580 KB |
Output is correct |
29 |
Correct |
6 ms |
10508 KB |
Output is correct |
30 |
Correct |
9 ms |
10708 KB |
Output is correct |
31 |
Correct |
6 ms |
10500 KB |
Output is correct |
32 |
Correct |
6 ms |
10324 KB |
Output is correct |
33 |
Correct |
6 ms |
10324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
4 ms |
9812 KB |
Output is correct |
5 |
Correct |
4 ms |
9732 KB |
Output is correct |
6 |
Correct |
6 ms |
10508 KB |
Output is correct |
7 |
Correct |
5 ms |
10508 KB |
Output is correct |
8 |
Correct |
6 ms |
10452 KB |
Output is correct |
9 |
Correct |
6 ms |
10348 KB |
Output is correct |
10 |
Correct |
5 ms |
10324 KB |
Output is correct |
11 |
Correct |
6 ms |
10452 KB |
Output is correct |
12 |
Correct |
6 ms |
10440 KB |
Output is correct |
13 |
Correct |
6 ms |
10580 KB |
Output is correct |
14 |
Correct |
6 ms |
10324 KB |
Output is correct |
15 |
Correct |
6 ms |
10580 KB |
Output is correct |
16 |
Correct |
6 ms |
10580 KB |
Output is correct |
17 |
Correct |
6 ms |
10508 KB |
Output is correct |
18 |
Correct |
9 ms |
10708 KB |
Output is correct |
19 |
Correct |
6 ms |
10500 KB |
Output is correct |
20 |
Correct |
6 ms |
10324 KB |
Output is correct |
21 |
Correct |
6 ms |
10324 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
6 ms |
9728 KB |
Output is correct |
24 |
Correct |
7 ms |
10724 KB |
Output is correct |
25 |
Correct |
8 ms |
10756 KB |
Output is correct |
26 |
Correct |
6 ms |
10708 KB |
Output is correct |
27 |
Correct |
6 ms |
9772 KB |
Output is correct |
28 |
Correct |
7 ms |
10480 KB |
Output is correct |
29 |
Correct |
6 ms |
10400 KB |
Output is correct |
30 |
Correct |
6 ms |
10376 KB |
Output is correct |
31 |
Correct |
5 ms |
10508 KB |
Output is correct |
32 |
Correct |
6 ms |
10324 KB |
Output is correct |
33 |
Correct |
48 ms |
43864 KB |
Output is correct |
34 |
Correct |
47 ms |
43980 KB |
Output is correct |
35 |
Correct |
6 ms |
10708 KB |
Output is correct |
36 |
Correct |
42 ms |
36076 KB |
Output is correct |
37 |
Correct |
50 ms |
36136 KB |
Output is correct |
38 |
Correct |
50 ms |
30540 KB |
Output is correct |
39 |
Correct |
47 ms |
37372 KB |
Output is correct |
40 |
Correct |
41 ms |
31356 KB |
Output is correct |
41 |
Correct |
49 ms |
30672 KB |
Output is correct |
42 |
Correct |
47 ms |
37312 KB |
Output is correct |
43 |
Correct |
63 ms |
31600 KB |
Output is correct |
44 |
Correct |
53 ms |
40332 KB |
Output is correct |
45 |
Correct |
48 ms |
35392 KB |
Output is correct |
46 |
Correct |
48 ms |
34936 KB |
Output is correct |
47 |
Correct |
49 ms |
34840 KB |
Output is correct |
48 |
Correct |
48 ms |
34972 KB |
Output is correct |
49 |
Correct |
50 ms |
34296 KB |
Output is correct |
50 |
Correct |
46 ms |
34764 KB |
Output is correct |
51 |
Correct |
126 ms |
49348 KB |
Output is correct |
52 |
Correct |
143 ms |
50088 KB |
Output is correct |
53 |
Correct |
93 ms |
47036 KB |
Output is correct |
54 |
Correct |
52 ms |
36844 KB |
Output is correct |
55 |
Correct |
53 ms |
36916 KB |
Output is correct |
56 |
Correct |
86 ms |
39468 KB |
Output is correct |
57 |
Correct |
67 ms |
33296 KB |
Output is correct |
58 |
Correct |
51 ms |
30528 KB |
Output is correct |
59 |
Correct |
164 ms |
36684 KB |
Output is correct |
60 |
Correct |
73 ms |
33172 KB |
Output is correct |
61 |
Correct |
86 ms |
41924 KB |
Output is correct |
62 |
Correct |
85 ms |
33532 KB |
Output is correct |
63 |
Correct |
79 ms |
42548 KB |
Output is correct |
64 |
Correct |
69 ms |
36740 KB |
Output is correct |
65 |
Correct |
53 ms |
35960 KB |
Output is correct |
66 |
Correct |
59 ms |
37360 KB |
Output is correct |
67 |
Correct |
64 ms |
37196 KB |
Output is correct |
68 |
Correct |
79 ms |
36516 KB |
Output is correct |
69 |
Correct |
78 ms |
39152 KB |
Output is correct |
70 |
Correct |
42 ms |
23008 KB |
Output is correct |
71 |
Correct |
81 ms |
37196 KB |
Output is correct |