#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector <int> v[200005],v1,v2[200005];
int d1,b1,cnt1,fr[200005],cnt[200005],dep[200005];
void dfs(int x,int d) {
dep[x]=d;
for(int i=0;i<v[x].size();i++) {
dfs(v[x][i],d+1);
}
}
void dfs1(int x) {
if(dep[x]>=d1) fr[x]=1;
cnt1+=fr[x];
for(int i=0;i<v[x].size();i++) {
dfs1(v[x][i]);
}
}
void dfs2(int x) {
if(b1) return;
int mx=-1,idx;
for(int i=0;i<v[x].size();i++) {
dfs2(v[x][i]);
if(b1) return;
fr[x]+=fr[v[x][i]];
if((int)v2[v[x][i]].size()>mx) {
mx=v2[v[x][i]].size();
idx=i;
}
}
if(!v[x].empty()) {
swap(v2[x],v2[v[x][idx]]);
for(int i=0;i<v[x].size();i++) {
for(int j=0;j<v2[v[x][i]].size();j++) v2[x].push_back(v2[v[x][i]][j]);
v2[v[x][i]].clear();
}
}
sort(v2[x].begin(),v2[x].end());
if(!v2[x].empty() && v2[x].back()-dep[x]>=dep[x]-1) {
b1=1;
return;
}
while(!v2[x].empty() && fr[x]>0) {
v2[x].pop_back();
fr[x]-=1;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m,a=0,a1=0;
cin >> n >> m;
for(int i=1;i<n;i++) {
int x;
cin >> x;
a+=(x==i);
v[x-1].push_back(i);
}
for(int i=0;i<m;i++) {
int x;
cin >> x;
if(x==1) a1=1;
v1.push_back(x-1);
}
if(a==n-1) {
if(a1) cout << 1;
else cout << n-m+1;
return 0;
}
dfs(0,1);
int lo=2,hi=n;
while(lo<hi) {
int mid=(lo+hi+1)/2;
b1=0;
cnt1=0;
d1=mid;
//cout << "---" << d1 << "\n\n";
for(int i=0;i<n;i++) {
fr[i]=0;
cnt[i]=0;
v2[i].clear();
}
dfs1(0);
if(cnt1<m) {
hi=mid-1;
continue;
}
for(int i=0;i<m;i++) {
cnt[v1[i]]=1;
v2[v1[i]].push_back(dep[v1[i]]);
}
dfs2(0);
for(int i=0;i<n;i++) {
if(!v2[i].empty()) b1=1;
}
if(!b1) lo=mid;
else hi=mid-1;
}
cout << lo;
return 0;
}
/*
__
/\ \
/ \ \
/ /\ \ \
/ / /\ \ \
/ / /__\_\ \
/ / /________\
\/___________/
*/
Compilation message
mokesciai.cpp: In function 'void dfs(int, int)':
mokesciai.cpp:9:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for(int i=0;i<v[x].size();i++) {
| ~^~~~~~~~~~~~
mokesciai.cpp: In function 'void dfs1(int)':
mokesciai.cpp:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i=0;i<v[x].size();i++) {
| ~^~~~~~~~~~~~
mokesciai.cpp: In function 'void dfs2(int)':
mokesciai.cpp:23:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=0;i<v[x].size();i++) {
| ~^~~~~~~~~~~~
mokesciai.cpp:34:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i=0;i<v[x].size();i++) {
| ~^~~~~~~~~~~~
mokesciai.cpp:35:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int j=0;j<v2[v[x][i]].size();j++) v2[x].push_back(v2[v[x][i]][j]);
| ~^~~~~~~~~~~~~~~~~~~
mokesciai.cpp:33:25: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
33 | swap(v2[x],v2[v[x][idx]]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9948 KB |
Output is correct |
4 |
Correct |
6 ms |
9884 KB |
Output is correct |
5 |
Correct |
6 ms |
9940 KB |
Output is correct |
6 |
Correct |
33 ms |
17160 KB |
Output is correct |
7 |
Correct |
39 ms |
17308 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
55 ms |
19264 KB |
Output is correct |
10 |
Correct |
50 ms |
19396 KB |
Output is correct |
11 |
Correct |
39 ms |
18320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
17160 KB |
Output is correct |
2 |
Correct |
39 ms |
17308 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
10096 KB |
Output is correct |
6 |
Correct |
8 ms |
9988 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
8 ms |
10088 KB |
Output is correct |
9 |
Correct |
9 ms |
9868 KB |
Output is correct |
10 |
Correct |
124 ms |
24956 KB |
Output is correct |
11 |
Correct |
119 ms |
24952 KB |
Output is correct |
12 |
Correct |
222 ms |
15272 KB |
Output is correct |
13 |
Correct |
142 ms |
27108 KB |
Output is correct |
14 |
Correct |
73 ms |
16204 KB |
Output is correct |
15 |
Correct |
218 ms |
15616 KB |
Output is correct |
16 |
Correct |
198 ms |
27096 KB |
Output is correct |
17 |
Correct |
315 ms |
16448 KB |
Output is correct |
18 |
Correct |
201 ms |
30796 KB |
Output is correct |
19 |
Correct |
160 ms |
20828 KB |
Output is correct |
20 |
Correct |
208 ms |
19844 KB |
Output is correct |
21 |
Correct |
214 ms |
19680 KB |
Output is correct |
22 |
Correct |
212 ms |
19844 KB |
Output is correct |
23 |
Correct |
185 ms |
19256 KB |
Output is correct |
24 |
Correct |
179 ms |
19532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9948 KB |
Output is correct |
4 |
Correct |
6 ms |
9884 KB |
Output is correct |
5 |
Correct |
6 ms |
9940 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
10096 KB |
Output is correct |
8 |
Correct |
8 ms |
9988 KB |
Output is correct |
9 |
Correct |
7 ms |
9812 KB |
Output is correct |
10 |
Correct |
8 ms |
10088 KB |
Output is correct |
11 |
Correct |
9 ms |
9868 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9724 KB |
Output is correct |
14 |
Correct |
5 ms |
9680 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
7 ms |
9684 KB |
Output is correct |
18 |
Correct |
14 ms |
10120 KB |
Output is correct |
19 |
Correct |
14 ms |
10068 KB |
Output is correct |
20 |
Correct |
8 ms |
10172 KB |
Output is correct |
21 |
Correct |
9 ms |
9940 KB |
Output is correct |
22 |
Correct |
7 ms |
9812 KB |
Output is correct |
23 |
Correct |
11 ms |
10068 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
8 ms |
10196 KB |
Output is correct |
26 |
Correct |
8 ms |
9940 KB |
Output is correct |
27 |
Correct |
89 ms |
10456 KB |
Output is correct |
28 |
Correct |
17 ms |
10320 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
10068 KB |
Output is correct |
31 |
Correct |
8 ms |
10068 KB |
Output is correct |
32 |
Correct |
8 ms |
9992 KB |
Output is correct |
33 |
Correct |
10 ms |
10236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9724 KB |
Output is correct |
2 |
Correct |
5 ms |
9680 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
14 ms |
10120 KB |
Output is correct |
7 |
Correct |
14 ms |
10068 KB |
Output is correct |
8 |
Correct |
8 ms |
10172 KB |
Output is correct |
9 |
Correct |
9 ms |
9940 KB |
Output is correct |
10 |
Correct |
7 ms |
9812 KB |
Output is correct |
11 |
Correct |
11 ms |
10068 KB |
Output is correct |
12 |
Correct |
7 ms |
9940 KB |
Output is correct |
13 |
Correct |
8 ms |
10196 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
89 ms |
10456 KB |
Output is correct |
16 |
Correct |
17 ms |
10320 KB |
Output is correct |
17 |
Correct |
8 ms |
9940 KB |
Output is correct |
18 |
Correct |
7 ms |
10068 KB |
Output is correct |
19 |
Correct |
8 ms |
10068 KB |
Output is correct |
20 |
Correct |
8 ms |
9992 KB |
Output is correct |
21 |
Correct |
10 ms |
10236 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
7 ms |
9684 KB |
Output is correct |
24 |
Correct |
6 ms |
9948 KB |
Output is correct |
25 |
Correct |
6 ms |
9884 KB |
Output is correct |
26 |
Correct |
6 ms |
9940 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
7 ms |
10096 KB |
Output is correct |
29 |
Correct |
8 ms |
9988 KB |
Output is correct |
30 |
Correct |
7 ms |
9812 KB |
Output is correct |
31 |
Correct |
8 ms |
10088 KB |
Output is correct |
32 |
Correct |
9 ms |
9868 KB |
Output is correct |
33 |
Correct |
33 ms |
17160 KB |
Output is correct |
34 |
Correct |
39 ms |
17308 KB |
Output is correct |
35 |
Correct |
6 ms |
9812 KB |
Output is correct |
36 |
Correct |
124 ms |
24956 KB |
Output is correct |
37 |
Correct |
119 ms |
24952 KB |
Output is correct |
38 |
Correct |
222 ms |
15272 KB |
Output is correct |
39 |
Correct |
142 ms |
27108 KB |
Output is correct |
40 |
Correct |
73 ms |
16204 KB |
Output is correct |
41 |
Correct |
218 ms |
15616 KB |
Output is correct |
42 |
Correct |
198 ms |
27096 KB |
Output is correct |
43 |
Correct |
315 ms |
16448 KB |
Output is correct |
44 |
Correct |
201 ms |
30796 KB |
Output is correct |
45 |
Correct |
160 ms |
20828 KB |
Output is correct |
46 |
Correct |
208 ms |
19844 KB |
Output is correct |
47 |
Correct |
214 ms |
19680 KB |
Output is correct |
48 |
Correct |
212 ms |
19844 KB |
Output is correct |
49 |
Correct |
185 ms |
19256 KB |
Output is correct |
50 |
Correct |
179 ms |
19532 KB |
Output is correct |
51 |
Correct |
55 ms |
19264 KB |
Output is correct |
52 |
Correct |
50 ms |
19396 KB |
Output is correct |
53 |
Correct |
39 ms |
18320 KB |
Output is correct |
54 |
Execution timed out |
2074 ms |
26948 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |