#include<bits/stdc++.h>
using namespace std;
const int MAXN=200005,LOG=18;
vector<int> adj[MAXN],que[MAXN];
int pa[LOG][MAXN],depth[MAXN],ST[MAXN],EN[MAXN],preorder[MAXN],idx;
bool coin[MAXN];
int anc(int u,int naik){
for(int i=LOG-1;i>=0;i--){
if(naik&(1<<i))u=pa[i][u];
}
return u;
}
void dfs(int now){
idx++;
ST[now]=idx;
preorder[idx]=now;
for(auto nxt : adj[now])dfs(nxt);
EN[now]=idx;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n,m,p,a;
cin >> n >> m;
for(int i=2;i<=n;i++){
cin >> p;
adj[p].push_back(i);
depth[i]=depth[p]+1;
pa[0][i]=p;
for(int j=1;j<LOG;j++)pa[j][i]=pa[j-1][pa[j-1][i]];
}
dfs(1);
for(int i=1;i<=m;i++){
cin >> a;
coin[a]=true;
}
if(coin[1]){
cout << 1 << '\n';
return 0;
}
for(int i=1;i<=n;i++){
if(coin[i]){
int atas=anc(i,(depth[i]-1)/2);
que[ST[atas]].push_back(EN[atas]+1);
}
}
int ki=0,ka=n,res;
while(ki<=ka){
int mid=(ki+ka)/2;
priority_queue<int,vector<int>,greater<int> > PQ;
bool yes=true;
for(int i=1;i<=n;i++){
for(auto isi : que[i])PQ.push(isi);
if(!PQ.empty() && depth[preorder[i]]>=mid){
if(PQ.top()<=i)yes=false;
PQ.pop();
}
}
if(yes && PQ.empty()){
res=mid;
ki=mid+1;
}
else ka=mid-1;
}
cout << res+1 << '\n';
return 0;
}
Compilation message
mokesciai.cpp: In function 'int main()':
mokesciai.cpp:68:19: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout << res+1 << '\n';
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9856 KB |
Output is correct |
3 |
Correct |
9 ms |
10752 KB |
Output is correct |
4 |
Correct |
11 ms |
10752 KB |
Output is correct |
5 |
Correct |
10 ms |
10752 KB |
Output is correct |
6 |
Correct |
71 ms |
40696 KB |
Output is correct |
7 |
Correct |
69 ms |
40696 KB |
Output is correct |
8 |
Correct |
9 ms |
10624 KB |
Output is correct |
9 |
Correct |
205 ms |
45936 KB |
Output is correct |
10 |
Correct |
221 ms |
46944 KB |
Output is correct |
11 |
Correct |
147 ms |
43964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9856 KB |
Output is correct |
2 |
Correct |
9 ms |
10496 KB |
Output is correct |
3 |
Correct |
7 ms |
10496 KB |
Output is correct |
4 |
Correct |
9 ms |
10368 KB |
Output is correct |
5 |
Correct |
7 ms |
10496 KB |
Output is correct |
6 |
Correct |
9 ms |
10368 KB |
Output is correct |
7 |
Correct |
71 ms |
40696 KB |
Output is correct |
8 |
Correct |
69 ms |
40696 KB |
Output is correct |
9 |
Correct |
9 ms |
10624 KB |
Output is correct |
10 |
Correct |
69 ms |
34980 KB |
Output is correct |
11 |
Correct |
65 ms |
34852 KB |
Output is correct |
12 |
Correct |
76 ms |
30188 KB |
Output is correct |
13 |
Correct |
69 ms |
35704 KB |
Output is correct |
14 |
Correct |
64 ms |
31224 KB |
Output is correct |
15 |
Correct |
100 ms |
30456 KB |
Output is correct |
16 |
Correct |
70 ms |
35704 KB |
Output is correct |
17 |
Correct |
143 ms |
31356 KB |
Output is correct |
18 |
Correct |
69 ms |
38264 KB |
Output is correct |
19 |
Correct |
70 ms |
34860 KB |
Output is correct |
20 |
Correct |
74 ms |
34680 KB |
Output is correct |
21 |
Correct |
79 ms |
34552 KB |
Output is correct |
22 |
Correct |
76 ms |
34680 KB |
Output is correct |
23 |
Correct |
78 ms |
34296 KB |
Output is correct |
24 |
Correct |
70 ms |
34424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
6 ms |
9856 KB |
Output is correct |
5 |
Correct |
6 ms |
9856 KB |
Output is correct |
6 |
Correct |
9 ms |
10496 KB |
Output is correct |
7 |
Correct |
8 ms |
10496 KB |
Output is correct |
8 |
Correct |
8 ms |
10496 KB |
Output is correct |
9 |
Correct |
8 ms |
10496 KB |
Output is correct |
10 |
Correct |
8 ms |
10368 KB |
Output is correct |
11 |
Correct |
10 ms |
10496 KB |
Output is correct |
12 |
Correct |
9 ms |
10496 KB |
Output is correct |
13 |
Correct |
15 ms |
10624 KB |
Output is correct |
14 |
Correct |
9 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10624 KB |
Output is correct |
16 |
Correct |
8 ms |
10624 KB |
Output is correct |
17 |
Correct |
10 ms |
10496 KB |
Output is correct |
18 |
Correct |
11 ms |
10752 KB |
Output is correct |
19 |
Correct |
8 ms |
10496 KB |
Output is correct |
20 |
Correct |
8 ms |
10368 KB |
Output is correct |
21 |
Correct |
12 ms |
10368 KB |
Output is correct |
22 |
Correct |
6 ms |
9856 KB |
Output is correct |
23 |
Correct |
6 ms |
9856 KB |
Output is correct |
24 |
Correct |
9 ms |
10752 KB |
Output is correct |
25 |
Correct |
11 ms |
10752 KB |
Output is correct |
26 |
Correct |
10 ms |
10752 KB |
Output is correct |
27 |
Correct |
7 ms |
9856 KB |
Output is correct |
28 |
Correct |
9 ms |
10496 KB |
Output is correct |
29 |
Correct |
7 ms |
10496 KB |
Output is correct |
30 |
Correct |
9 ms |
10368 KB |
Output is correct |
31 |
Correct |
7 ms |
10496 KB |
Output is correct |
32 |
Correct |
9 ms |
10368 KB |
Output is correct |
33 |
Correct |
9 ms |
10624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
6 ms |
9856 KB |
Output is correct |
5 |
Correct |
6 ms |
9856 KB |
Output is correct |
6 |
Correct |
9 ms |
10496 KB |
Output is correct |
7 |
Correct |
8 ms |
10496 KB |
Output is correct |
8 |
Correct |
8 ms |
10496 KB |
Output is correct |
9 |
Correct |
8 ms |
10496 KB |
Output is correct |
10 |
Correct |
8 ms |
10368 KB |
Output is correct |
11 |
Correct |
10 ms |
10496 KB |
Output is correct |
12 |
Correct |
9 ms |
10496 KB |
Output is correct |
13 |
Correct |
15 ms |
10624 KB |
Output is correct |
14 |
Correct |
9 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10624 KB |
Output is correct |
16 |
Correct |
8 ms |
10624 KB |
Output is correct |
17 |
Correct |
10 ms |
10496 KB |
Output is correct |
18 |
Correct |
11 ms |
10752 KB |
Output is correct |
19 |
Correct |
8 ms |
10496 KB |
Output is correct |
20 |
Correct |
8 ms |
10368 KB |
Output is correct |
21 |
Correct |
12 ms |
10368 KB |
Output is correct |
22 |
Correct |
6 ms |
9856 KB |
Output is correct |
23 |
Correct |
6 ms |
9856 KB |
Output is correct |
24 |
Correct |
9 ms |
10752 KB |
Output is correct |
25 |
Correct |
11 ms |
10752 KB |
Output is correct |
26 |
Correct |
10 ms |
10752 KB |
Output is correct |
27 |
Correct |
7 ms |
9856 KB |
Output is correct |
28 |
Correct |
9 ms |
10496 KB |
Output is correct |
29 |
Correct |
7 ms |
10496 KB |
Output is correct |
30 |
Correct |
9 ms |
10368 KB |
Output is correct |
31 |
Correct |
7 ms |
10496 KB |
Output is correct |
32 |
Correct |
9 ms |
10368 KB |
Output is correct |
33 |
Correct |
71 ms |
40696 KB |
Output is correct |
34 |
Correct |
69 ms |
40696 KB |
Output is correct |
35 |
Correct |
9 ms |
10624 KB |
Output is correct |
36 |
Correct |
69 ms |
34980 KB |
Output is correct |
37 |
Correct |
65 ms |
34852 KB |
Output is correct |
38 |
Correct |
76 ms |
30188 KB |
Output is correct |
39 |
Correct |
69 ms |
35704 KB |
Output is correct |
40 |
Correct |
64 ms |
31224 KB |
Output is correct |
41 |
Correct |
100 ms |
30456 KB |
Output is correct |
42 |
Correct |
70 ms |
35704 KB |
Output is correct |
43 |
Correct |
143 ms |
31356 KB |
Output is correct |
44 |
Correct |
69 ms |
38264 KB |
Output is correct |
45 |
Correct |
70 ms |
34860 KB |
Output is correct |
46 |
Correct |
74 ms |
34680 KB |
Output is correct |
47 |
Correct |
79 ms |
34552 KB |
Output is correct |
48 |
Correct |
76 ms |
34680 KB |
Output is correct |
49 |
Correct |
78 ms |
34296 KB |
Output is correct |
50 |
Correct |
70 ms |
34424 KB |
Output is correct |
51 |
Correct |
205 ms |
45936 KB |
Output is correct |
52 |
Correct |
221 ms |
46944 KB |
Output is correct |
53 |
Correct |
147 ms |
43964 KB |
Output is correct |
54 |
Correct |
89 ms |
35824 KB |
Output is correct |
55 |
Correct |
85 ms |
35736 KB |
Output is correct |
56 |
Correct |
152 ms |
38088 KB |
Output is correct |
57 |
Correct |
124 ms |
33376 KB |
Output is correct |
58 |
Correct |
81 ms |
30192 KB |
Output is correct |
59 |
Correct |
258 ms |
36132 KB |
Output is correct |
60 |
Correct |
128 ms |
33284 KB |
Output is correct |
61 |
Correct |
362 ms |
40524 KB |
Output is correct |
62 |
Correct |
187 ms |
33600 KB |
Output is correct |
63 |
Correct |
124 ms |
40652 KB |
Output is correct |
64 |
Correct |
120 ms |
36692 KB |
Output is correct |
65 |
Correct |
104 ms |
36048 KB |
Output is correct |
66 |
Correct |
129 ms |
37424 KB |
Output is correct |
67 |
Correct |
138 ms |
37244 KB |
Output is correct |
68 |
Correct |
114 ms |
36524 KB |
Output is correct |
69 |
Correct |
193 ms |
39256 KB |
Output is correct |
70 |
Correct |
75 ms |
22984 KB |
Output is correct |
71 |
Correct |
127 ms |
36020 KB |
Output is correct |