#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int logn = 19;
vector <int> g[maxn];
int par[maxn];
int dep[maxn];
int vis[maxn];
int anc[logn + 1][maxn];
int lift(int x, int depth) {
for(int i = logn; i >= 0; i--) {
if(dep[x] - (1 << i) > depth) {
x = anc[i][x];
}
}
return x;
}
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);
anc[0][i] = par[i];
}
for(int i = 1; i <= logn; i++) {
for(int j = 1; j <= n; j++) {
anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
}
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;
i.second = lift(i.second, top);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
120 ms |
5888 KB |
Output is correct |
4 |
Correct |
119 ms |
6016 KB |
Output is correct |
5 |
Correct |
143 ms |
6016 KB |
Output is correct |
6 |
Correct |
165 ms |
39800 KB |
Output is correct |
7 |
Correct |
163 ms |
40952 KB |
Output is correct |
8 |
Correct |
7 ms |
6016 KB |
Output is correct |
9 |
Execution timed out |
2051 ms |
36968 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5760 KB |
Output is correct |
3 |
Correct |
7 ms |
5888 KB |
Output is correct |
4 |
Correct |
6 ms |
5632 KB |
Output is correct |
5 |
Correct |
7 ms |
5760 KB |
Output is correct |
6 |
Correct |
7 ms |
5632 KB |
Output is correct |
7 |
Correct |
165 ms |
39800 KB |
Output is correct |
8 |
Correct |
163 ms |
40952 KB |
Output is correct |
9 |
Correct |
7 ms |
6016 KB |
Output is correct |
10 |
Correct |
145 ms |
29044 KB |
Output is correct |
11 |
Correct |
144 ms |
29044 KB |
Output is correct |
12 |
Correct |
129 ms |
24688 KB |
Output is correct |
13 |
Correct |
158 ms |
31440 KB |
Output is correct |
14 |
Correct |
140 ms |
25336 KB |
Output is correct |
15 |
Correct |
132 ms |
24952 KB |
Output is correct |
16 |
Correct |
157 ms |
32248 KB |
Output is correct |
17 |
Correct |
180 ms |
25464 KB |
Output is correct |
18 |
Correct |
155 ms |
34680 KB |
Output is correct |
19 |
Correct |
157 ms |
29208 KB |
Output is correct |
20 |
Correct |
150 ms |
28536 KB |
Output is correct |
21 |
Correct |
150 ms |
28536 KB |
Output is correct |
22 |
Correct |
151 ms |
28664 KB |
Output is correct |
23 |
Correct |
159 ms |
28408 KB |
Output is correct |
24 |
Correct |
149 ms |
28536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
67 ms |
5788 KB |
Output is correct |
7 |
Correct |
33 ms |
5760 KB |
Output is correct |
8 |
Correct |
145 ms |
5880 KB |
Output is correct |
9 |
Correct |
12 ms |
5632 KB |
Output is correct |
10 |
Correct |
6 ms |
5632 KB |
Output is correct |
11 |
Correct |
17 ms |
5760 KB |
Output is correct |
12 |
Correct |
16 ms |
5632 KB |
Output is correct |
13 |
Correct |
119 ms |
5880 KB |
Output is correct |
14 |
Correct |
12 ms |
5760 KB |
Output is correct |
15 |
Correct |
81 ms |
6016 KB |
Output is correct |
16 |
Correct |
46 ms |
5912 KB |
Output is correct |
17 |
Correct |
17 ms |
5760 KB |
Output is correct |
18 |
Correct |
66 ms |
5888 KB |
Output is correct |
19 |
Correct |
9 ms |
5760 KB |
Output is correct |
20 |
Correct |
92 ms |
5632 KB |
Output is correct |
21 |
Correct |
27 ms |
5632 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
120 ms |
5888 KB |
Output is correct |
25 |
Correct |
119 ms |
6016 KB |
Output is correct |
26 |
Correct |
143 ms |
6016 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
7 ms |
5760 KB |
Output is correct |
29 |
Correct |
7 ms |
5888 KB |
Output is correct |
30 |
Correct |
6 ms |
5632 KB |
Output is correct |
31 |
Correct |
7 ms |
5760 KB |
Output is correct |
32 |
Correct |
7 ms |
5632 KB |
Output is correct |
33 |
Correct |
7 ms |
6016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
67 ms |
5788 KB |
Output is correct |
7 |
Correct |
33 ms |
5760 KB |
Output is correct |
8 |
Correct |
145 ms |
5880 KB |
Output is correct |
9 |
Correct |
12 ms |
5632 KB |
Output is correct |
10 |
Correct |
6 ms |
5632 KB |
Output is correct |
11 |
Correct |
17 ms |
5760 KB |
Output is correct |
12 |
Correct |
16 ms |
5632 KB |
Output is correct |
13 |
Correct |
119 ms |
5880 KB |
Output is correct |
14 |
Correct |
12 ms |
5760 KB |
Output is correct |
15 |
Correct |
81 ms |
6016 KB |
Output is correct |
16 |
Correct |
46 ms |
5912 KB |
Output is correct |
17 |
Correct |
17 ms |
5760 KB |
Output is correct |
18 |
Correct |
66 ms |
5888 KB |
Output is correct |
19 |
Correct |
9 ms |
5760 KB |
Output is correct |
20 |
Correct |
92 ms |
5632 KB |
Output is correct |
21 |
Correct |
27 ms |
5632 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
120 ms |
5888 KB |
Output is correct |
25 |
Correct |
119 ms |
6016 KB |
Output is correct |
26 |
Correct |
143 ms |
6016 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
7 ms |
5760 KB |
Output is correct |
29 |
Correct |
7 ms |
5888 KB |
Output is correct |
30 |
Correct |
6 ms |
5632 KB |
Output is correct |
31 |
Correct |
7 ms |
5760 KB |
Output is correct |
32 |
Correct |
7 ms |
5632 KB |
Output is correct |
33 |
Correct |
165 ms |
39800 KB |
Output is correct |
34 |
Correct |
163 ms |
40952 KB |
Output is correct |
35 |
Correct |
7 ms |
6016 KB |
Output is correct |
36 |
Correct |
145 ms |
29044 KB |
Output is correct |
37 |
Correct |
144 ms |
29044 KB |
Output is correct |
38 |
Correct |
129 ms |
24688 KB |
Output is correct |
39 |
Correct |
158 ms |
31440 KB |
Output is correct |
40 |
Correct |
140 ms |
25336 KB |
Output is correct |
41 |
Correct |
132 ms |
24952 KB |
Output is correct |
42 |
Correct |
157 ms |
32248 KB |
Output is correct |
43 |
Correct |
180 ms |
25464 KB |
Output is correct |
44 |
Correct |
155 ms |
34680 KB |
Output is correct |
45 |
Correct |
157 ms |
29208 KB |
Output is correct |
46 |
Correct |
150 ms |
28536 KB |
Output is correct |
47 |
Correct |
150 ms |
28536 KB |
Output is correct |
48 |
Correct |
151 ms |
28664 KB |
Output is correct |
49 |
Correct |
159 ms |
28408 KB |
Output is correct |
50 |
Correct |
149 ms |
28536 KB |
Output is correct |
51 |
Execution timed out |
2051 ms |
36968 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |