#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6+6, inf = LLONG_MAX;
int n, m, sz, ans, dol[N], dol2[N], up[N], dep[N];
vector<int> path, adj[N];
bitset<N> vis;
set<pair<int, int>> st[N];
void dfs3(int x, int idx, int d) {
if (vis[x]) return;
vis[x] = 1;
auto it = st[idx].find(make_pair(-dep[x], x));
if (it != st[idx].end()) st[idx].erase(it);
for (int& y : adj[x]) {
if (y == up[x]) continue;
dfs3(y, idx, d+1);
}
}
void dfs2(int x) {
st[x].insert(make_pair(-dep[x], x));
for (int& y : adj[x]) {
if (y == up[x]) continue;
dfs2(y);
if (st[y].size() > st[x].size()) swap(st[x], st[y]);
for (pair<int, int> p : st[y]) {
st[x].insert(p);
}
st[y].clear();
}
//assert(dol2[x] <= st[x].size());
while (dol2[x] > 0) {
//cerr << x << " " << dol2[x] << endl;
dol2[x]--;
auto it = st[x].begin();
int d = it->first;
int y = it->second;
st[x].erase(it);
ans = min(ans, -d);
st[x].insert(make_pair(d+1, up[y]));
dfs3(y, x, 0);
}
}
void dfs(int x, int u, int d) {
up[x] = u;
dep[x] = d;
path.push_back(x);
if (dol[x] && x) {
dol[x]--;
int sz = path.size();
dol2[path[sz - sz/2]]++;
}
for (int& y : adj[x]) {
if (y == u) continue;
dfs(y, x, d+1);
}
path.pop_back();
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
sz = 0;
ans = inf;
memset(dol, 0, sizeof(dol));
memset(dol2, 0, sizeof(dol2));
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);
dfs2(0);
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
91992 KB |
Output is correct |
2 |
Correct |
17 ms |
91880 KB |
Output is correct |
3 |
Correct |
20 ms |
93384 KB |
Output is correct |
4 |
Correct |
20 ms |
93384 KB |
Output is correct |
5 |
Correct |
19 ms |
93368 KB |
Output is correct |
6 |
Correct |
112 ms |
145644 KB |
Output is correct |
7 |
Correct |
112 ms |
145600 KB |
Output is correct |
8 |
Correct |
20 ms |
93380 KB |
Output is correct |
9 |
Correct |
135 ms |
145848 KB |
Output is correct |
10 |
Correct |
142 ms |
145724 KB |
Output is correct |
11 |
Correct |
129 ms |
145616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
145644 KB |
Output is correct |
2 |
Correct |
112 ms |
145600 KB |
Output is correct |
3 |
Correct |
20 ms |
93380 KB |
Output is correct |
4 |
Correct |
18 ms |
91992 KB |
Output is correct |
5 |
Correct |
19 ms |
93020 KB |
Output is correct |
6 |
Correct |
20 ms |
92764 KB |
Output is correct |
7 |
Correct |
20 ms |
92508 KB |
Output is correct |
8 |
Correct |
19 ms |
92688 KB |
Output is correct |
9 |
Correct |
24 ms |
92500 KB |
Output is correct |
10 |
Correct |
120 ms |
131604 KB |
Output is correct |
11 |
Correct |
109 ms |
131644 KB |
Output is correct |
12 |
Correct |
171 ms |
113924 KB |
Output is correct |
13 |
Correct |
97 ms |
129992 KB |
Output is correct |
14 |
Correct |
185 ms |
118600 KB |
Output is correct |
15 |
Correct |
165 ms |
113996 KB |
Output is correct |
16 |
Correct |
97 ms |
129984 KB |
Output is correct |
17 |
Correct |
260 ms |
115312 KB |
Output is correct |
18 |
Correct |
116 ms |
137488 KB |
Output is correct |
19 |
Correct |
132 ms |
115544 KB |
Output is correct |
20 |
Correct |
108 ms |
113292 KB |
Output is correct |
21 |
Correct |
108 ms |
113232 KB |
Output is correct |
22 |
Correct |
110 ms |
113492 KB |
Output is correct |
23 |
Correct |
102 ms |
112804 KB |
Output is correct |
24 |
Correct |
109 ms |
112724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
91992 KB |
Output is correct |
2 |
Correct |
17 ms |
91880 KB |
Output is correct |
3 |
Correct |
20 ms |
93384 KB |
Output is correct |
4 |
Correct |
20 ms |
93384 KB |
Output is correct |
5 |
Correct |
19 ms |
93368 KB |
Output is correct |
6 |
Correct |
18 ms |
91992 KB |
Output is correct |
7 |
Correct |
19 ms |
93020 KB |
Output is correct |
8 |
Correct |
20 ms |
92764 KB |
Output is correct |
9 |
Correct |
20 ms |
92508 KB |
Output is correct |
10 |
Correct |
19 ms |
92688 KB |
Output is correct |
11 |
Correct |
24 ms |
92500 KB |
Output is correct |
12 |
Correct |
20 ms |
93380 KB |
Output is correct |
13 |
Correct |
19 ms |
91996 KB |
Output is correct |
14 |
Correct |
17 ms |
91992 KB |
Output is correct |
15 |
Correct |
18 ms |
91880 KB |
Output is correct |
16 |
Correct |
17 ms |
91996 KB |
Output is correct |
17 |
Correct |
20 ms |
92092 KB |
Output is correct |
18 |
Correct |
20 ms |
93020 KB |
Output is correct |
19 |
Correct |
33 ms |
92636 KB |
Output is correct |
20 |
Correct |
20 ms |
93012 KB |
Output is correct |
21 |
Correct |
22 ms |
92252 KB |
Output is correct |
22 |
Correct |
20 ms |
92508 KB |
Output is correct |
23 |
Correct |
19 ms |
92968 KB |
Output is correct |
24 |
Correct |
20 ms |
92252 KB |
Output is correct |
25 |
Correct |
21 ms |
92764 KB |
Output is correct |
26 |
Correct |
21 ms |
92252 KB |
Output is correct |
27 |
Correct |
19 ms |
93068 KB |
Output is correct |
28 |
Correct |
20 ms |
92764 KB |
Output is correct |
29 |
Correct |
19 ms |
92508 KB |
Output is correct |
30 |
Correct |
19 ms |
92764 KB |
Output is correct |
31 |
Correct |
19 ms |
92252 KB |
Output is correct |
32 |
Correct |
23 ms |
92588 KB |
Output is correct |
33 |
Correct |
19 ms |
92508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
91996 KB |
Output is correct |
2 |
Correct |
17 ms |
91992 KB |
Output is correct |
3 |
Correct |
18 ms |
91880 KB |
Output is correct |
4 |
Correct |
17 ms |
91996 KB |
Output is correct |
5 |
Correct |
20 ms |
92092 KB |
Output is correct |
6 |
Correct |
20 ms |
93020 KB |
Output is correct |
7 |
Correct |
33 ms |
92636 KB |
Output is correct |
8 |
Correct |
20 ms |
93012 KB |
Output is correct |
9 |
Correct |
22 ms |
92252 KB |
Output is correct |
10 |
Correct |
20 ms |
92508 KB |
Output is correct |
11 |
Correct |
19 ms |
92968 KB |
Output is correct |
12 |
Correct |
20 ms |
92252 KB |
Output is correct |
13 |
Correct |
21 ms |
92764 KB |
Output is correct |
14 |
Correct |
21 ms |
92252 KB |
Output is correct |
15 |
Correct |
19 ms |
93068 KB |
Output is correct |
16 |
Correct |
20 ms |
92764 KB |
Output is correct |
17 |
Correct |
19 ms |
92508 KB |
Output is correct |
18 |
Correct |
19 ms |
92764 KB |
Output is correct |
19 |
Correct |
19 ms |
92252 KB |
Output is correct |
20 |
Correct |
23 ms |
92588 KB |
Output is correct |
21 |
Correct |
19 ms |
92508 KB |
Output is correct |
22 |
Correct |
18 ms |
91992 KB |
Output is correct |
23 |
Correct |
17 ms |
91880 KB |
Output is correct |
24 |
Correct |
20 ms |
93384 KB |
Output is correct |
25 |
Correct |
20 ms |
93384 KB |
Output is correct |
26 |
Correct |
19 ms |
93368 KB |
Output is correct |
27 |
Correct |
18 ms |
91992 KB |
Output is correct |
28 |
Correct |
19 ms |
93020 KB |
Output is correct |
29 |
Correct |
20 ms |
92764 KB |
Output is correct |
30 |
Correct |
20 ms |
92508 KB |
Output is correct |
31 |
Correct |
19 ms |
92688 KB |
Output is correct |
32 |
Correct |
24 ms |
92500 KB |
Output is correct |
33 |
Correct |
112 ms |
145644 KB |
Output is correct |
34 |
Correct |
112 ms |
145600 KB |
Output is correct |
35 |
Correct |
20 ms |
93380 KB |
Output is correct |
36 |
Correct |
120 ms |
131604 KB |
Output is correct |
37 |
Correct |
109 ms |
131644 KB |
Output is correct |
38 |
Correct |
171 ms |
113924 KB |
Output is correct |
39 |
Correct |
97 ms |
129992 KB |
Output is correct |
40 |
Correct |
185 ms |
118600 KB |
Output is correct |
41 |
Correct |
165 ms |
113996 KB |
Output is correct |
42 |
Correct |
97 ms |
129984 KB |
Output is correct |
43 |
Correct |
260 ms |
115312 KB |
Output is correct |
44 |
Correct |
116 ms |
137488 KB |
Output is correct |
45 |
Correct |
132 ms |
115544 KB |
Output is correct |
46 |
Correct |
108 ms |
113292 KB |
Output is correct |
47 |
Correct |
108 ms |
113232 KB |
Output is correct |
48 |
Correct |
110 ms |
113492 KB |
Output is correct |
49 |
Correct |
102 ms |
112804 KB |
Output is correct |
50 |
Correct |
109 ms |
112724 KB |
Output is correct |
51 |
Correct |
135 ms |
145848 KB |
Output is correct |
52 |
Correct |
142 ms |
145724 KB |
Output is correct |
53 |
Correct |
129 ms |
145616 KB |
Output is correct |
54 |
Correct |
125 ms |
131676 KB |
Output is correct |
55 |
Correct |
116 ms |
126544 KB |
Output is correct |
56 |
Correct |
114 ms |
130112 KB |
Output is correct |
57 |
Correct |
159 ms |
110480 KB |
Output is correct |
58 |
Correct |
157 ms |
113988 KB |
Output is correct |
59 |
Correct |
134 ms |
118032 KB |
Output is correct |
60 |
Correct |
164 ms |
110964 KB |
Output is correct |
61 |
Correct |
130 ms |
128832 KB |
Output is correct |
62 |
Correct |
243 ms |
111700 KB |
Output is correct |
63 |
Correct |
123 ms |
132592 KB |
Output is correct |
64 |
Correct |
111 ms |
113372 KB |
Output is correct |
65 |
Correct |
133 ms |
111884 KB |
Output is correct |
66 |
Correct |
104 ms |
108112 KB |
Output is correct |
67 |
Correct |
115 ms |
110416 KB |
Output is correct |
68 |
Correct |
129 ms |
112340 KB |
Output is correct |
69 |
Correct |
99 ms |
104784 KB |
Output is correct |
70 |
Correct |
109 ms |
104876 KB |
Output is correct |
71 |
Correct |
140 ms |
120264 KB |
Output is correct |