#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
vector <int> g[maxn];
int par[maxn];
int dep[maxn];
int vis[maxn];
void dfs(int x) {
for(int i : g[x]) {
dep[i] = 1 + dep[x];
dfs(i);
}
}
pair <int, int> getFarthest(int x) {
if(vis[x]) return make_pair(0, x);
pair <int, int> ans (dep[x], x);
for(int i : g[x]) {
ans = max(ans, getFarthest(i));
}
return ans;
}
int main() {
int n, m;
cin >> n >> m;
for(int i = 2; i <= n; i++) {
cin >> par[i];
g[par[i]].push_back(i);
}
dep[1] = 1;
dfs(1);
vector <pair <int, int>> v;
for(int i = 1; i <= m; i++) {
int x;
cin >> x;
v.emplace_back(dep[x], x);
if(x == 1) {
cout << 1 << endl;
exit(0);
}
}
for(auto &i : v) {
int top = (i.first + 1) / 2;
while(top + 1 < dep[i.second]) {
i.second = par[i.second];
}
i.first = dep[i.second];
}
sort(v.begin(), v.end(), greater <pair <int, int>> ());
int ans = n;
for(auto &i : v) {
auto j = getFarthest(i.second);
vis[j.second] = true;
ans = min(ans, j.first);
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
129 ms |
5504 KB |
Output is correct |
4 |
Correct |
136 ms |
5504 KB |
Output is correct |
5 |
Correct |
148 ms |
5504 KB |
Output is correct |
6 |
Correct |
148 ms |
24568 KB |
Output is correct |
7 |
Correct |
148 ms |
26620 KB |
Output is correct |
8 |
Correct |
7 ms |
5504 KB |
Output is correct |
9 |
Execution timed out |
2060 ms |
23656 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
6 ms |
5376 KB |
Output is correct |
3 |
Correct |
6 ms |
5248 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5248 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
148 ms |
24568 KB |
Output is correct |
8 |
Correct |
148 ms |
26620 KB |
Output is correct |
9 |
Correct |
7 ms |
5504 KB |
Output is correct |
10 |
Correct |
134 ms |
14580 KB |
Output is correct |
11 |
Correct |
133 ms |
14708 KB |
Output is correct |
12 |
Correct |
116 ms |
9836 KB |
Output is correct |
13 |
Correct |
145 ms |
17144 KB |
Output is correct |
14 |
Correct |
130 ms |
11000 KB |
Output is correct |
15 |
Correct |
122 ms |
10104 KB |
Output is correct |
16 |
Correct |
146 ms |
17788 KB |
Output is correct |
17 |
Correct |
168 ms |
11128 KB |
Output is correct |
18 |
Correct |
147 ms |
20216 KB |
Output is correct |
19 |
Correct |
148 ms |
14712 KB |
Output is correct |
20 |
Correct |
139 ms |
14200 KB |
Output is correct |
21 |
Correct |
140 ms |
14200 KB |
Output is correct |
22 |
Correct |
143 ms |
14192 KB |
Output is correct |
23 |
Correct |
149 ms |
13944 KB |
Output is correct |
24 |
Correct |
139 ms |
14072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
70 ms |
5376 KB |
Output is correct |
7 |
Correct |
36 ms |
5248 KB |
Output is correct |
8 |
Correct |
152 ms |
5368 KB |
Output is correct |
9 |
Correct |
12 ms |
5248 KB |
Output is correct |
10 |
Correct |
7 ms |
5248 KB |
Output is correct |
11 |
Correct |
17 ms |
5248 KB |
Output is correct |
12 |
Correct |
16 ms |
5248 KB |
Output is correct |
13 |
Correct |
126 ms |
5376 KB |
Output is correct |
14 |
Correct |
12 ms |
5248 KB |
Output is correct |
15 |
Correct |
84 ms |
5376 KB |
Output is correct |
16 |
Correct |
50 ms |
5496 KB |
Output is correct |
17 |
Correct |
17 ms |
5376 KB |
Output is correct |
18 |
Correct |
74 ms |
5376 KB |
Output is correct |
19 |
Correct |
10 ms |
5248 KB |
Output is correct |
20 |
Correct |
94 ms |
5368 KB |
Output is correct |
21 |
Correct |
29 ms |
5248 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
4 ms |
4992 KB |
Output is correct |
24 |
Correct |
129 ms |
5504 KB |
Output is correct |
25 |
Correct |
136 ms |
5504 KB |
Output is correct |
26 |
Correct |
148 ms |
5504 KB |
Output is correct |
27 |
Correct |
3 ms |
4992 KB |
Output is correct |
28 |
Correct |
6 ms |
5376 KB |
Output is correct |
29 |
Correct |
6 ms |
5248 KB |
Output is correct |
30 |
Correct |
6 ms |
5120 KB |
Output is correct |
31 |
Correct |
7 ms |
5248 KB |
Output is correct |
32 |
Correct |
6 ms |
5120 KB |
Output is correct |
33 |
Correct |
7 ms |
5504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
70 ms |
5376 KB |
Output is correct |
7 |
Correct |
36 ms |
5248 KB |
Output is correct |
8 |
Correct |
152 ms |
5368 KB |
Output is correct |
9 |
Correct |
12 ms |
5248 KB |
Output is correct |
10 |
Correct |
7 ms |
5248 KB |
Output is correct |
11 |
Correct |
17 ms |
5248 KB |
Output is correct |
12 |
Correct |
16 ms |
5248 KB |
Output is correct |
13 |
Correct |
126 ms |
5376 KB |
Output is correct |
14 |
Correct |
12 ms |
5248 KB |
Output is correct |
15 |
Correct |
84 ms |
5376 KB |
Output is correct |
16 |
Correct |
50 ms |
5496 KB |
Output is correct |
17 |
Correct |
17 ms |
5376 KB |
Output is correct |
18 |
Correct |
74 ms |
5376 KB |
Output is correct |
19 |
Correct |
10 ms |
5248 KB |
Output is correct |
20 |
Correct |
94 ms |
5368 KB |
Output is correct |
21 |
Correct |
29 ms |
5248 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
4 ms |
4992 KB |
Output is correct |
24 |
Correct |
129 ms |
5504 KB |
Output is correct |
25 |
Correct |
136 ms |
5504 KB |
Output is correct |
26 |
Correct |
148 ms |
5504 KB |
Output is correct |
27 |
Correct |
3 ms |
4992 KB |
Output is correct |
28 |
Correct |
6 ms |
5376 KB |
Output is correct |
29 |
Correct |
6 ms |
5248 KB |
Output is correct |
30 |
Correct |
6 ms |
5120 KB |
Output is correct |
31 |
Correct |
7 ms |
5248 KB |
Output is correct |
32 |
Correct |
6 ms |
5120 KB |
Output is correct |
33 |
Correct |
148 ms |
24568 KB |
Output is correct |
34 |
Correct |
148 ms |
26620 KB |
Output is correct |
35 |
Correct |
7 ms |
5504 KB |
Output is correct |
36 |
Correct |
134 ms |
14580 KB |
Output is correct |
37 |
Correct |
133 ms |
14708 KB |
Output is correct |
38 |
Correct |
116 ms |
9836 KB |
Output is correct |
39 |
Correct |
145 ms |
17144 KB |
Output is correct |
40 |
Correct |
130 ms |
11000 KB |
Output is correct |
41 |
Correct |
122 ms |
10104 KB |
Output is correct |
42 |
Correct |
146 ms |
17788 KB |
Output is correct |
43 |
Correct |
168 ms |
11128 KB |
Output is correct |
44 |
Correct |
147 ms |
20216 KB |
Output is correct |
45 |
Correct |
148 ms |
14712 KB |
Output is correct |
46 |
Correct |
139 ms |
14200 KB |
Output is correct |
47 |
Correct |
140 ms |
14200 KB |
Output is correct |
48 |
Correct |
143 ms |
14192 KB |
Output is correct |
49 |
Correct |
149 ms |
13944 KB |
Output is correct |
50 |
Correct |
139 ms |
14072 KB |
Output is correct |
51 |
Execution timed out |
2060 ms |
23656 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |