#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 cnt[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);
}
}
int ans = INT_MAX;
void solve(int x, priority_queue <int> &s) {
s.push(dep[x]);
for(int i : g[x]) {
priority_queue <int> t;
solve(i, t);
if(t.size() > s.size()) t.swap(s);
while(not t.empty()) {
s.push(t.top());
t.pop();
}
}
for(int j = 0; j < cnt[x]; j++) {
ans = min(ans, s.top());
s.pop();
}
}
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 <int> v;
for(int i = 1; i <= m; i++) {
int x;
cin >> x;
v.emplace_back(x);
if(x == 1) {
cout << 1 << endl;
exit(0);
}
}
for(auto i : v) {
int top = (dep[i] + 1) / 2;
i = lift(i, top);
cnt[i] += 1;
}
priority_queue <int> s;
solve(1, s);
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 |
10 ms |
6400 KB |
Output is correct |
4 |
Correct |
11 ms |
6400 KB |
Output is correct |
5 |
Correct |
9 ms |
6400 KB |
Output is correct |
6 |
Correct |
181 ms |
53512 KB |
Output is correct |
7 |
Correct |
185 ms |
53496 KB |
Output is correct |
8 |
Correct |
8 ms |
6272 KB |
Output is correct |
9 |
Correct |
395 ms |
54632 KB |
Output is correct |
10 |
Correct |
476 ms |
57368 KB |
Output is correct |
11 |
Correct |
290 ms |
56176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5888 KB |
Output is correct |
3 |
Correct |
7 ms |
5888 KB |
Output is correct |
4 |
Correct |
7 ms |
5632 KB |
Output is correct |
5 |
Correct |
7 ms |
5888 KB |
Output is correct |
6 |
Correct |
8 ms |
5760 KB |
Output is correct |
7 |
Correct |
181 ms |
53512 KB |
Output is correct |
8 |
Correct |
185 ms |
53496 KB |
Output is correct |
9 |
Correct |
8 ms |
6272 KB |
Output is correct |
10 |
Correct |
166 ms |
37356 KB |
Output is correct |
11 |
Correct |
169 ms |
37348 KB |
Output is correct |
12 |
Correct |
172 ms |
26216 KB |
Output is correct |
13 |
Correct |
188 ms |
39288 KB |
Output is correct |
14 |
Correct |
228 ms |
26984 KB |
Output is correct |
15 |
Correct |
174 ms |
26356 KB |
Output is correct |
16 |
Correct |
182 ms |
39288 KB |
Output is correct |
17 |
Correct |
276 ms |
27044 KB |
Output is correct |
18 |
Correct |
187 ms |
44024 KB |
Output is correct |
19 |
Correct |
190 ms |
31128 KB |
Output is correct |
20 |
Correct |
183 ms |
29988 KB |
Output is correct |
21 |
Correct |
185 ms |
29788 KB |
Output is correct |
22 |
Correct |
184 ms |
29920 KB |
Output is correct |
23 |
Correct |
179 ms |
29424 KB |
Output is correct |
24 |
Correct |
179 ms |
29676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 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 |
8 ms |
5888 KB |
Output is correct |
7 |
Correct |
9 ms |
5888 KB |
Output is correct |
8 |
Correct |
9 ms |
6016 KB |
Output is correct |
9 |
Correct |
10 ms |
5632 KB |
Output is correct |
10 |
Correct |
7 ms |
5632 KB |
Output is correct |
11 |
Correct |
8 ms |
6016 KB |
Output is correct |
12 |
Correct |
8 ms |
5632 KB |
Output is correct |
13 |
Correct |
11 ms |
5888 KB |
Output is correct |
14 |
Correct |
10 ms |
5632 KB |
Output is correct |
15 |
Correct |
9 ms |
6144 KB |
Output is correct |
16 |
Correct |
9 ms |
6016 KB |
Output is correct |
17 |
Correct |
8 ms |
5760 KB |
Output is correct |
18 |
Correct |
14 ms |
6016 KB |
Output is correct |
19 |
Correct |
9 ms |
5760 KB |
Output is correct |
20 |
Correct |
9 ms |
5632 KB |
Output is correct |
21 |
Correct |
7 ms |
5760 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
10 ms |
6400 KB |
Output is correct |
25 |
Correct |
11 ms |
6400 KB |
Output is correct |
26 |
Correct |
9 ms |
6400 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
8 ms |
5888 KB |
Output is correct |
29 |
Correct |
7 ms |
5888 KB |
Output is correct |
30 |
Correct |
7 ms |
5632 KB |
Output is correct |
31 |
Correct |
7 ms |
5888 KB |
Output is correct |
32 |
Correct |
8 ms |
5760 KB |
Output is correct |
33 |
Correct |
8 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 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 |
8 ms |
5888 KB |
Output is correct |
7 |
Correct |
9 ms |
5888 KB |
Output is correct |
8 |
Correct |
9 ms |
6016 KB |
Output is correct |
9 |
Correct |
10 ms |
5632 KB |
Output is correct |
10 |
Correct |
7 ms |
5632 KB |
Output is correct |
11 |
Correct |
8 ms |
6016 KB |
Output is correct |
12 |
Correct |
8 ms |
5632 KB |
Output is correct |
13 |
Correct |
11 ms |
5888 KB |
Output is correct |
14 |
Correct |
10 ms |
5632 KB |
Output is correct |
15 |
Correct |
9 ms |
6144 KB |
Output is correct |
16 |
Correct |
9 ms |
6016 KB |
Output is correct |
17 |
Correct |
8 ms |
5760 KB |
Output is correct |
18 |
Correct |
14 ms |
6016 KB |
Output is correct |
19 |
Correct |
9 ms |
5760 KB |
Output is correct |
20 |
Correct |
9 ms |
5632 KB |
Output is correct |
21 |
Correct |
7 ms |
5760 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
10 ms |
6400 KB |
Output is correct |
25 |
Correct |
11 ms |
6400 KB |
Output is correct |
26 |
Correct |
9 ms |
6400 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
8 ms |
5888 KB |
Output is correct |
29 |
Correct |
7 ms |
5888 KB |
Output is correct |
30 |
Correct |
7 ms |
5632 KB |
Output is correct |
31 |
Correct |
7 ms |
5888 KB |
Output is correct |
32 |
Correct |
8 ms |
5760 KB |
Output is correct |
33 |
Correct |
181 ms |
53512 KB |
Output is correct |
34 |
Correct |
185 ms |
53496 KB |
Output is correct |
35 |
Correct |
8 ms |
6272 KB |
Output is correct |
36 |
Correct |
166 ms |
37356 KB |
Output is correct |
37 |
Correct |
169 ms |
37348 KB |
Output is correct |
38 |
Correct |
172 ms |
26216 KB |
Output is correct |
39 |
Correct |
188 ms |
39288 KB |
Output is correct |
40 |
Correct |
228 ms |
26984 KB |
Output is correct |
41 |
Correct |
174 ms |
26356 KB |
Output is correct |
42 |
Correct |
182 ms |
39288 KB |
Output is correct |
43 |
Correct |
276 ms |
27044 KB |
Output is correct |
44 |
Correct |
187 ms |
44024 KB |
Output is correct |
45 |
Correct |
190 ms |
31128 KB |
Output is correct |
46 |
Correct |
183 ms |
29988 KB |
Output is correct |
47 |
Correct |
185 ms |
29788 KB |
Output is correct |
48 |
Correct |
184 ms |
29920 KB |
Output is correct |
49 |
Correct |
179 ms |
29424 KB |
Output is correct |
50 |
Correct |
179 ms |
29676 KB |
Output is correct |
51 |
Correct |
395 ms |
54632 KB |
Output is correct |
52 |
Correct |
476 ms |
57368 KB |
Output is correct |
53 |
Correct |
290 ms |
56176 KB |
Output is correct |
54 |
Correct |
200 ms |
39016 KB |
Output is correct |
55 |
Correct |
199 ms |
39020 KB |
Output is correct |
56 |
Correct |
272 ms |
41460 KB |
Output is correct |
57 |
Correct |
272 ms |
28660 KB |
Output is correct |
58 |
Correct |
168 ms |
26808 KB |
Output is correct |
59 |
Correct |
305 ms |
34800 KB |
Output is correct |
60 |
Correct |
229 ms |
28528 KB |
Output is correct |
61 |
Correct |
439 ms |
42604 KB |
Output is correct |
62 |
Correct |
338 ms |
29316 KB |
Output is correct |
63 |
Correct |
286 ms |
46448 KB |
Output is correct |
64 |
Correct |
247 ms |
33332 KB |
Output is correct |
65 |
Correct |
231 ms |
31964 KB |
Output is correct |
66 |
Correct |
259 ms |
31984 KB |
Output is correct |
67 |
Correct |
263 ms |
32384 KB |
Output is correct |
68 |
Correct |
251 ms |
31600 KB |
Output is correct |
69 |
Correct |
290 ms |
32492 KB |
Output is correct |
70 |
Correct |
178 ms |
18676 KB |
Output is correct |
71 |
Correct |
269 ms |
37104 KB |
Output is correct |