#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int N = 2e5+5/*1e6+6*/, inf = INT_MAX;
int n, m, sz, ans, dol[N], up[N], dep[N], par[N], seg[2][N*4];
vector<int> path, leafs, lvl[N], adj[N];
pair<int, int> sg[N];
void build(int p=1, int l=0, int r=sz-1) {
if (l == r) {
seg[0][p] = leafs[l];
seg[1][p] = l;
return;
}
int mid = (l+r)/2;
build(p*2, l, mid);
build(p*2+1, mid+1, r);
seg[0][p] = max(seg[0][p*2], seg[0][p*2+1]);
seg[1][p] = (seg[0][p] == seg[0][p*2] ? seg[1][p*2] : seg[1][p*2+1]);
}
void update(int x, int p=1, int l=0, int r=sz-1) {
if (l == r) {
seg[0][p]--;
return;
}
int mid = (l+r)/2;
if (x <= mid) update(x, p*2, l, mid);
else update(x, p*2+1, mid+1, r);
seg[0][p] = max(seg[0][p*2], seg[0][p*2+1]);
seg[1][p] = (seg[0][p] == seg[0][p*2] ? seg[1][p*2] : seg[1][p*2+1]);
}
pair<int, int> query(int x, int y, int p=1, int l=0, int r=sz-1) {
if (x <= l && r <= y) {
return make_pair(seg[0][p], seg[1][p]);
}
int mid = (l+r)/2;
if (y <= mid) {
return query(x, y, p*2, l, mid);
}
else if (x >= mid+1) {
return query(x, y, p*2+1, mid+1, r);
}
else {
pair<int, int> pr1 = query(x, y, p*2, l, mid);
pair<int, int> pr2 = query(x, y, p*2+1, mid+1, r);
if (pr1.first > pr2.first) return pr1;
else return pr2;
}
}
pair<int, int> dfs(int x, int u, int d) {
up[x] = u;
dep[x] = d;
lvl[d].push_back(x);
path.push_back(x);
if (dol[x] && x) {
dol[x]--;
int sz = path.size();
dol[path[sz - sz/2]]++;
}
if (adj[x].size() == 1 && x) {
leafs.push_back(dep[x]);
sg[x] = make_pair(sz, sz);
sz++;
path.pop_back();
return sg[x];
}
int mn = inf;
int mx = -inf;
for (int& y : adj[x]) {
if (y == u) continue;
pair<int, int> child = dfs(y, x, d+1);
mn = min(mn, child.first);
mx = max(mx, child.second);
}
path.pop_back();
return sg[x] = make_pair(mn, mx);
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
sz = 0;
ans = inf;
memset(dol, 0, sizeof(dol));
cin >> n >> m;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
x--;
adj[x].push_back(i);
adj[i].push_back(x);
}
bool ok = 1;
for (int i = 0; i < m; i++) {
int x;
cin >> x;
x--;
dol[x]++;
if (!x) ok = 0;
}
if (n == 1 || !ok) {
cout << "1\n";
return 0;
}
dfs(0, 0, 1);
build();
for (int i = n; i >= 1; i--) {
for (int& x : lvl[i]) {
while (dol[x] > 0) {
pair<int, int> pr = query(sg[x].first, sg[x].second);
dol[x]--;
ans = min(ans, pr.first);
update(pr.second);
}
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17500 KB |
Output is correct |
2 |
Correct |
3 ms |
17664 KB |
Output is correct |
3 |
Correct |
6 ms |
18268 KB |
Output is correct |
4 |
Correct |
5 ms |
18268 KB |
Output is correct |
5 |
Correct |
4 ms |
18268 KB |
Output is correct |
6 |
Correct |
57 ms |
47560 KB |
Output is correct |
7 |
Correct |
57 ms |
47516 KB |
Output is correct |
8 |
Correct |
4 ms |
18268 KB |
Output is correct |
9 |
Correct |
61 ms |
47536 KB |
Output is correct |
10 |
Correct |
67 ms |
47560 KB |
Output is correct |
11 |
Correct |
63 ms |
47604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
47560 KB |
Output is correct |
2 |
Correct |
57 ms |
47516 KB |
Output is correct |
3 |
Correct |
4 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
17500 KB |
Output is correct |
5 |
Correct |
5 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18096 KB |
Output is correct |
7 |
Correct |
4 ms |
17756 KB |
Output is correct |
8 |
Correct |
5 ms |
18044 KB |
Output is correct |
9 |
Correct |
4 ms |
17952 KB |
Output is correct |
10 |
Correct |
44 ms |
38632 KB |
Output is correct |
11 |
Correct |
44 ms |
38724 KB |
Output is correct |
12 |
Correct |
57 ms |
29924 KB |
Output is correct |
13 |
Correct |
54 ms |
39772 KB |
Output is correct |
14 |
Correct |
36 ms |
29640 KB |
Output is correct |
15 |
Correct |
52 ms |
29868 KB |
Output is correct |
16 |
Correct |
51 ms |
39836 KB |
Output is correct |
17 |
Correct |
68 ms |
29944 KB |
Output is correct |
18 |
Correct |
45 ms |
39700 KB |
Output is correct |
19 |
Correct |
40 ms |
28664 KB |
Output is correct |
20 |
Correct |
41 ms |
27492 KB |
Output is correct |
21 |
Correct |
40 ms |
27224 KB |
Output is correct |
22 |
Correct |
40 ms |
27476 KB |
Output is correct |
23 |
Correct |
43 ms |
27268 KB |
Output is correct |
24 |
Correct |
39 ms |
26960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17500 KB |
Output is correct |
2 |
Correct |
3 ms |
17664 KB |
Output is correct |
3 |
Correct |
6 ms |
18268 KB |
Output is correct |
4 |
Correct |
5 ms |
18268 KB |
Output is correct |
5 |
Correct |
4 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
17500 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18096 KB |
Output is correct |
9 |
Correct |
4 ms |
17756 KB |
Output is correct |
10 |
Correct |
5 ms |
18044 KB |
Output is correct |
11 |
Correct |
4 ms |
17952 KB |
Output is correct |
12 |
Correct |
4 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
17500 KB |
Output is correct |
14 |
Correct |
3 ms |
17500 KB |
Output is correct |
15 |
Correct |
3 ms |
17500 KB |
Output is correct |
16 |
Correct |
3 ms |
17500 KB |
Output is correct |
17 |
Correct |
4 ms |
17500 KB |
Output is correct |
18 |
Correct |
4 ms |
18008 KB |
Output is correct |
19 |
Correct |
4 ms |
18012 KB |
Output is correct |
20 |
Correct |
5 ms |
18176 KB |
Output is correct |
21 |
Correct |
5 ms |
17756 KB |
Output is correct |
22 |
Correct |
5 ms |
17756 KB |
Output is correct |
23 |
Incorrect |
4 ms |
18012 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
17500 KB |
Output is correct |
2 |
Correct |
3 ms |
17500 KB |
Output is correct |
3 |
Correct |
3 ms |
17500 KB |
Output is correct |
4 |
Correct |
3 ms |
17500 KB |
Output is correct |
5 |
Correct |
4 ms |
17500 KB |
Output is correct |
6 |
Correct |
4 ms |
18008 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18176 KB |
Output is correct |
9 |
Correct |
5 ms |
17756 KB |
Output is correct |
10 |
Correct |
5 ms |
17756 KB |
Output is correct |
11 |
Incorrect |
4 ms |
18012 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |