#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5+10;
const int maxLog = 32;
int n, m;
vector<int> adj[maxN];
int dep[maxN]={0};
int par[maxN][maxLog]={0};
int best[maxN];
void dfs(int v, int u){
dep[v]=dep[u]+1;
par[v][0]=u;
for(auto i : adj[v]){
if(i==u)continue;
dfs(i, v);
}
}
int lift(int p, int x){
for(int i = 0; i < maxLog; i++){
if(x&(1<<i)){
p=par[p][i];
}
}
return p;
}
bool isPossible;
int possible(int v, int u, int val){
int counter=0;
for(auto i : adj[v]){
if(i==u)continue;
counter+=possible(i, v, val);
}
if(dep[v]>=val-1)counter++;
if(counter<best[v]){isPossible=false;/*cout<<v<<" "<<val<<" "<<best[v]<<" "<<counter<<"bbb"<<endl;*/};
counter-=best[v];
return counter;
}
int solve(){
int l=0, h=n;
while(l<h-1){
int mid = l+(h-l)/2;
//cout<<mid<<" "<<l<<" "<<h<<endl;
isPossible=true;
possible(1, 0, mid);
if(isPossible)l=mid;
else h=mid-1;
}
isPossible=true;
possible(1, 0, h+1);
if(isPossible)return h+1;
isPossible=true;
possible(1, 0, h);
if(isPossible)return h;
isPossible=true;
possible(1, 0, h-1);
if(isPossible)return h-1;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(nullptr);
cin>>n>>m;
for(int i = 2; i <= n; i++){
int aa;
cin>>aa;
adj[aa].push_back(i);
adj[i].push_back(aa);
}
dep[0]=-1;
par[0][0]=0;
dfs(1, 0);
for(int i = 1; i < maxLog; i++)for(int x = 1; x <= n; x++)par[x][i]=par[par[x][i-1]][i-1];
for(int i = 1; i <= m; i++){
int aa;
cin>>aa;
//cout<<lift(aa, (dep[i]-1)/2)<<" "<<(dep[i]-1)/2<<endl;
best[lift(aa, (dep[aa]-1)/2)]++;
}//cout<<endl;
cout<<solve();
return 0;
}
Compilation message
mokesciai.cpp: In function 'int solve()':
mokesciai.cpp:64:1: warning: control reaches end of non-void function [-Wreturn-type]
64 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
6 ms |
6188 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6188 KB |
Output is correct |
6 |
Correct |
188 ms |
53984 KB |
Output is correct |
7 |
Correct |
196 ms |
54084 KB |
Output is correct |
8 |
Correct |
5 ms |
6128 KB |
Output is correct |
9 |
Correct |
319 ms |
55208 KB |
Output is correct |
10 |
Correct |
361 ms |
55652 KB |
Output is correct |
11 |
Correct |
266 ms |
54664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
53984 KB |
Output is correct |
2 |
Correct |
196 ms |
54084 KB |
Output is correct |
3 |
Correct |
5 ms |
6128 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
5 ms |
5988 KB |
Output is correct |
6 |
Correct |
5 ms |
5964 KB |
Output is correct |
7 |
Correct |
5 ms |
5804 KB |
Output is correct |
8 |
Correct |
5 ms |
5936 KB |
Output is correct |
9 |
Correct |
5 ms |
5828 KB |
Output is correct |
10 |
Correct |
145 ms |
44840 KB |
Output is correct |
11 |
Correct |
159 ms |
45008 KB |
Output is correct |
12 |
Correct |
301 ms |
38328 KB |
Output is correct |
13 |
Correct |
190 ms |
46088 KB |
Output is correct |
14 |
Correct |
133 ms |
38228 KB |
Output is correct |
15 |
Correct |
315 ms |
38560 KB |
Output is correct |
16 |
Correct |
186 ms |
46096 KB |
Output is correct |
17 |
Correct |
427 ms |
38380 KB |
Output is correct |
18 |
Correct |
190 ms |
47728 KB |
Output is correct |
19 |
Correct |
181 ms |
39264 KB |
Output is correct |
20 |
Correct |
208 ms |
38688 KB |
Output is correct |
21 |
Correct |
197 ms |
38384 KB |
Output is correct |
22 |
Correct |
202 ms |
38584 KB |
Output is correct |
23 |
Correct |
224 ms |
38340 KB |
Output is correct |
24 |
Correct |
203 ms |
38276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
6 ms |
6188 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6188 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
5 ms |
5988 KB |
Output is correct |
8 |
Correct |
5 ms |
5964 KB |
Output is correct |
9 |
Correct |
5 ms |
5804 KB |
Output is correct |
10 |
Correct |
5 ms |
5936 KB |
Output is correct |
11 |
Correct |
5 ms |
5828 KB |
Output is correct |
12 |
Correct |
5 ms |
6128 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
5932 KB |
Output is correct |
19 |
Correct |
5 ms |
5928 KB |
Output is correct |
20 |
Correct |
5 ms |
6040 KB |
Output is correct |
21 |
Correct |
5 ms |
5836 KB |
Output is correct |
22 |
Correct |
5 ms |
5804 KB |
Output is correct |
23 |
Correct |
5 ms |
5932 KB |
Output is correct |
24 |
Correct |
5 ms |
5832 KB |
Output is correct |
25 |
Correct |
6 ms |
5964 KB |
Output is correct |
26 |
Correct |
5 ms |
5804 KB |
Output is correct |
27 |
Correct |
5 ms |
6060 KB |
Output is correct |
28 |
Correct |
5 ms |
6056 KB |
Output is correct |
29 |
Correct |
5 ms |
5836 KB |
Output is correct |
30 |
Correct |
8 ms |
6092 KB |
Output is correct |
31 |
Correct |
6 ms |
5836 KB |
Output is correct |
32 |
Correct |
5 ms |
5736 KB |
Output is correct |
33 |
Correct |
4 ms |
5600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
5932 KB |
Output is correct |
7 |
Correct |
5 ms |
5928 KB |
Output is correct |
8 |
Correct |
5 ms |
6040 KB |
Output is correct |
9 |
Correct |
5 ms |
5836 KB |
Output is correct |
10 |
Correct |
5 ms |
5804 KB |
Output is correct |
11 |
Correct |
5 ms |
5932 KB |
Output is correct |
12 |
Correct |
5 ms |
5832 KB |
Output is correct |
13 |
Correct |
6 ms |
5964 KB |
Output is correct |
14 |
Correct |
5 ms |
5804 KB |
Output is correct |
15 |
Correct |
5 ms |
6060 KB |
Output is correct |
16 |
Correct |
5 ms |
6056 KB |
Output is correct |
17 |
Correct |
5 ms |
5836 KB |
Output is correct |
18 |
Correct |
8 ms |
6092 KB |
Output is correct |
19 |
Correct |
6 ms |
5836 KB |
Output is correct |
20 |
Correct |
5 ms |
5736 KB |
Output is correct |
21 |
Correct |
4 ms |
5600 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
2 ms |
4940 KB |
Output is correct |
24 |
Correct |
6 ms |
6188 KB |
Output is correct |
25 |
Correct |
5 ms |
6220 KB |
Output is correct |
26 |
Correct |
5 ms |
6188 KB |
Output is correct |
27 |
Correct |
3 ms |
5012 KB |
Output is correct |
28 |
Correct |
5 ms |
5988 KB |
Output is correct |
29 |
Correct |
5 ms |
5964 KB |
Output is correct |
30 |
Correct |
5 ms |
5804 KB |
Output is correct |
31 |
Correct |
5 ms |
5936 KB |
Output is correct |
32 |
Correct |
5 ms |
5828 KB |
Output is correct |
33 |
Correct |
188 ms |
53984 KB |
Output is correct |
34 |
Correct |
196 ms |
54084 KB |
Output is correct |
35 |
Correct |
5 ms |
6128 KB |
Output is correct |
36 |
Correct |
145 ms |
44840 KB |
Output is correct |
37 |
Correct |
159 ms |
45008 KB |
Output is correct |
38 |
Correct |
301 ms |
38328 KB |
Output is correct |
39 |
Correct |
190 ms |
46088 KB |
Output is correct |
40 |
Correct |
133 ms |
38228 KB |
Output is correct |
41 |
Correct |
315 ms |
38560 KB |
Output is correct |
42 |
Correct |
186 ms |
46096 KB |
Output is correct |
43 |
Correct |
427 ms |
38380 KB |
Output is correct |
44 |
Correct |
190 ms |
47728 KB |
Output is correct |
45 |
Correct |
181 ms |
39264 KB |
Output is correct |
46 |
Correct |
208 ms |
38688 KB |
Output is correct |
47 |
Correct |
197 ms |
38384 KB |
Output is correct |
48 |
Correct |
202 ms |
38584 KB |
Output is correct |
49 |
Correct |
224 ms |
38340 KB |
Output is correct |
50 |
Correct |
203 ms |
38276 KB |
Output is correct |
51 |
Correct |
319 ms |
55208 KB |
Output is correct |
52 |
Correct |
361 ms |
55652 KB |
Output is correct |
53 |
Correct |
266 ms |
54664 KB |
Output is correct |
54 |
Correct |
152 ms |
45084 KB |
Output is correct |
55 |
Correct |
156 ms |
45292 KB |
Output is correct |
56 |
Correct |
214 ms |
47164 KB |
Output is correct |
57 |
Correct |
175 ms |
38760 KB |
Output is correct |
58 |
Correct |
282 ms |
38368 KB |
Output is correct |
59 |
Correct |
237 ms |
42436 KB |
Output is correct |
60 |
Correct |
368 ms |
39716 KB |
Output is correct |
61 |
Correct |
335 ms |
47556 KB |
Output is correct |
62 |
Correct |
443 ms |
39384 KB |
Output is correct |
63 |
Correct |
241 ms |
48548 KB |
Output is correct |
64 |
Correct |
209 ms |
39876 KB |
Output is correct |
65 |
Correct |
225 ms |
39064 KB |
Output is correct |
66 |
Correct |
260 ms |
39656 KB |
Output is correct |
67 |
Correct |
240 ms |
39316 KB |
Output is correct |
68 |
Correct |
252 ms |
39084 KB |
Output is correct |
69 |
Correct |
269 ms |
39592 KB |
Output is correct |
70 |
Correct |
91 ms |
22752 KB |
Output is correct |
71 |
Correct |
176 ms |
42176 KB |
Output is correct |