#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int MAXN = 200005;
const int INF = 1e8;
int N, M;
int A[MAXN];
vector<int> adj[MAXN];
int depth[MAXN];
int lift[MAXN][20];
void DfsInit(int u) {
for (int j = 1; j < 20; j++) {
lift[u][j] = lift[lift[u][j - 1]][j - 1];
}
for (auto v : adj[u]) {
lift[v][0] = u;
depth[v] = depth[u] + 1;
DfsInit(v);
}
}
int dp[MAXN];
multiset<int> leaf[MAXN];
void Dfs(int u) {
dp[u] = INF;
leaf[u].emplace(depth[u]);
for (auto v : adj[u]) {
Dfs(v);
if (leaf[u].size() < leaf[v].size()) swap(leaf[u], leaf[v]);
for (auto i : leaf[v]) leaf[u].emplace(i);
leaf[v].clear();
dp[u] = min(dp[u], dp[v]);
}
while (A[u] > 0) {
A[u]--;
auto it = prev(end(leaf[u]));
dp[u] = min(dp[u], *it);
leaf[u].erase(it);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> M;
for (int i = 2; i <= N; i++) {
int p;
cin >> p;
adj[p].emplace_back(i);
}
for (int i = 0; i < M; i++) {
int x;
cin >> x;
A[x]++;
}
if (A[1]) {
cout << 1 << "\n";
return 0;
}
DfsInit(1);
for (int i = 1; i <= N; i++) {
if (A[i]) {
int u = i;
for (int j = 19; j >= 0; j--) {
if (lift[u][j] != 0 && depth[lift[u][j]] > depth[i] - depth[lift[u][j]]) {
u = lift[u][j];
}
}
A[u]++;
A[i]--;
}
}
Dfs(1);
cout << 1 + dp[1] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
15872 KB |
Output is correct |
4 |
Correct |
12 ms |
15872 KB |
Output is correct |
5 |
Correct |
11 ms |
15872 KB |
Output is correct |
6 |
Correct |
149 ms |
69368 KB |
Output is correct |
7 |
Correct |
149 ms |
69372 KB |
Output is correct |
8 |
Correct |
11 ms |
15872 KB |
Output is correct |
9 |
Correct |
185 ms |
70136 KB |
Output is correct |
10 |
Correct |
198 ms |
70136 KB |
Output is correct |
11 |
Correct |
175 ms |
69752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
15488 KB |
Output is correct |
3 |
Correct |
11 ms |
15488 KB |
Output is correct |
4 |
Correct |
11 ms |
15232 KB |
Output is correct |
5 |
Correct |
11 ms |
15488 KB |
Output is correct |
6 |
Correct |
14 ms |
15360 KB |
Output is correct |
7 |
Correct |
149 ms |
69368 KB |
Output is correct |
8 |
Correct |
149 ms |
69372 KB |
Output is correct |
9 |
Correct |
11 ms |
15872 KB |
Output is correct |
10 |
Correct |
143 ms |
57764 KB |
Output is correct |
11 |
Correct |
145 ms |
57764 KB |
Output is correct |
12 |
Correct |
187 ms |
43628 KB |
Output is correct |
13 |
Correct |
153 ms |
56568 KB |
Output is correct |
14 |
Correct |
264 ms |
47480 KB |
Output is correct |
15 |
Correct |
198 ms |
43768 KB |
Output is correct |
16 |
Correct |
148 ms |
56572 KB |
Output is correct |
17 |
Correct |
303 ms |
45304 KB |
Output is correct |
18 |
Correct |
158 ms |
64248 KB |
Output is correct |
19 |
Correct |
159 ms |
49272 KB |
Output is correct |
20 |
Correct |
155 ms |
47868 KB |
Output is correct |
21 |
Correct |
155 ms |
47736 KB |
Output is correct |
22 |
Correct |
157 ms |
47868 KB |
Output is correct |
23 |
Correct |
158 ms |
47224 KB |
Output is correct |
24 |
Correct |
175 ms |
47480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
15616 KB |
Output is correct |
7 |
Correct |
17 ms |
15488 KB |
Output is correct |
8 |
Correct |
11 ms |
15488 KB |
Output is correct |
9 |
Correct |
14 ms |
15104 KB |
Output is correct |
10 |
Correct |
13 ms |
15232 KB |
Output is correct |
11 |
Correct |
10 ms |
15488 KB |
Output is correct |
12 |
Correct |
13 ms |
15132 KB |
Output is correct |
13 |
Correct |
14 ms |
15360 KB |
Output is correct |
14 |
Correct |
12 ms |
15232 KB |
Output is correct |
15 |
Correct |
11 ms |
15616 KB |
Output is correct |
16 |
Correct |
12 ms |
15632 KB |
Output is correct |
17 |
Correct |
11 ms |
15360 KB |
Output is correct |
18 |
Correct |
11 ms |
15488 KB |
Output is correct |
19 |
Correct |
13 ms |
15232 KB |
Output is correct |
20 |
Correct |
11 ms |
15232 KB |
Output is correct |
21 |
Correct |
11 ms |
15232 KB |
Output is correct |
22 |
Correct |
8 ms |
14464 KB |
Output is correct |
23 |
Correct |
8 ms |
14464 KB |
Output is correct |
24 |
Correct |
11 ms |
15872 KB |
Output is correct |
25 |
Correct |
12 ms |
15872 KB |
Output is correct |
26 |
Correct |
11 ms |
15872 KB |
Output is correct |
27 |
Correct |
8 ms |
14464 KB |
Output is correct |
28 |
Correct |
11 ms |
15488 KB |
Output is correct |
29 |
Correct |
11 ms |
15488 KB |
Output is correct |
30 |
Correct |
11 ms |
15232 KB |
Output is correct |
31 |
Correct |
11 ms |
15488 KB |
Output is correct |
32 |
Correct |
14 ms |
15360 KB |
Output is correct |
33 |
Correct |
11 ms |
15872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
15616 KB |
Output is correct |
7 |
Correct |
17 ms |
15488 KB |
Output is correct |
8 |
Correct |
11 ms |
15488 KB |
Output is correct |
9 |
Correct |
14 ms |
15104 KB |
Output is correct |
10 |
Correct |
13 ms |
15232 KB |
Output is correct |
11 |
Correct |
10 ms |
15488 KB |
Output is correct |
12 |
Correct |
13 ms |
15132 KB |
Output is correct |
13 |
Correct |
14 ms |
15360 KB |
Output is correct |
14 |
Correct |
12 ms |
15232 KB |
Output is correct |
15 |
Correct |
11 ms |
15616 KB |
Output is correct |
16 |
Correct |
12 ms |
15632 KB |
Output is correct |
17 |
Correct |
11 ms |
15360 KB |
Output is correct |
18 |
Correct |
11 ms |
15488 KB |
Output is correct |
19 |
Correct |
13 ms |
15232 KB |
Output is correct |
20 |
Correct |
11 ms |
15232 KB |
Output is correct |
21 |
Correct |
11 ms |
15232 KB |
Output is correct |
22 |
Correct |
8 ms |
14464 KB |
Output is correct |
23 |
Correct |
8 ms |
14464 KB |
Output is correct |
24 |
Correct |
11 ms |
15872 KB |
Output is correct |
25 |
Correct |
12 ms |
15872 KB |
Output is correct |
26 |
Correct |
11 ms |
15872 KB |
Output is correct |
27 |
Correct |
8 ms |
14464 KB |
Output is correct |
28 |
Correct |
11 ms |
15488 KB |
Output is correct |
29 |
Correct |
11 ms |
15488 KB |
Output is correct |
30 |
Correct |
11 ms |
15232 KB |
Output is correct |
31 |
Correct |
11 ms |
15488 KB |
Output is correct |
32 |
Correct |
14 ms |
15360 KB |
Output is correct |
33 |
Correct |
149 ms |
69368 KB |
Output is correct |
34 |
Correct |
149 ms |
69372 KB |
Output is correct |
35 |
Correct |
11 ms |
15872 KB |
Output is correct |
36 |
Correct |
143 ms |
57764 KB |
Output is correct |
37 |
Correct |
145 ms |
57764 KB |
Output is correct |
38 |
Correct |
187 ms |
43628 KB |
Output is correct |
39 |
Correct |
153 ms |
56568 KB |
Output is correct |
40 |
Correct |
264 ms |
47480 KB |
Output is correct |
41 |
Correct |
198 ms |
43768 KB |
Output is correct |
42 |
Correct |
148 ms |
56572 KB |
Output is correct |
43 |
Correct |
303 ms |
45304 KB |
Output is correct |
44 |
Correct |
158 ms |
64248 KB |
Output is correct |
45 |
Correct |
159 ms |
49272 KB |
Output is correct |
46 |
Correct |
155 ms |
47868 KB |
Output is correct |
47 |
Correct |
155 ms |
47736 KB |
Output is correct |
48 |
Correct |
157 ms |
47868 KB |
Output is correct |
49 |
Correct |
158 ms |
47224 KB |
Output is correct |
50 |
Correct |
175 ms |
47480 KB |
Output is correct |
51 |
Correct |
185 ms |
70136 KB |
Output is correct |
52 |
Correct |
198 ms |
70136 KB |
Output is correct |
53 |
Correct |
175 ms |
69752 KB |
Output is correct |
54 |
Correct |
149 ms |
58012 KB |
Output is correct |
55 |
Correct |
142 ms |
54308 KB |
Output is correct |
56 |
Correct |
161 ms |
56172 KB |
Output is correct |
57 |
Correct |
226 ms |
41592 KB |
Output is correct |
58 |
Correct |
174 ms |
43644 KB |
Output is correct |
59 |
Correct |
188 ms |
48632 KB |
Output is correct |
60 |
Correct |
202 ms |
42236 KB |
Output is correct |
61 |
Correct |
189 ms |
56312 KB |
Output is correct |
62 |
Correct |
300 ms |
43512 KB |
Output is correct |
63 |
Correct |
173 ms |
61304 KB |
Output is correct |
64 |
Correct |
169 ms |
48376 KB |
Output is correct |
65 |
Correct |
173 ms |
47480 KB |
Output is correct |
66 |
Correct |
175 ms |
44664 KB |
Output is correct |
67 |
Correct |
182 ms |
46456 KB |
Output is correct |
68 |
Correct |
183 ms |
47096 KB |
Output is correct |
69 |
Correct |
164 ms |
42228 KB |
Output is correct |
70 |
Correct |
152 ms |
32376 KB |
Output is correct |
71 |
Correct |
198 ms |
50296 KB |
Output is correct |