#include <iostream>
#include <set>
#include <vector>
#include <assert.h>
#include <map>
#include <list>
#define vi vector<int>
#define pub push_back
#define pii pair<int, int>
#define fst first
#define snd second
using namespace std;
int n, k, cnt[200001] = {}, mn = 1e9;
bool spc[200001];
list<int> adj[200001];
vi par;
void dfs(int u, int p)
{
int h = par.size();
par.push_back(u);
cnt[par[h / 2 + 1]] += spc[u];
spc[u] = 0;
for (auto v : adj[u])
{
// cout << u << " - " << v << "\n";
if (v != p) dfs(v, u);
}
par.pop_back();
}
map<int, int>* dfs2(int u, int p, int h)
{
map<int, int>* ret = new map<int, int>;
map<int, int>* rr;
(*ret)[-h]++;
for (auto v : adj[u])
{
if (v != p)
{
rr = dfs2(v, u, h + 1);
if (rr -> size() > ret -> size()) {swap(rr, ret);}
for (auto v : *rr) {(*ret)[v.fst] += v.snd;}
}
}
for (int i = 0; i < cnt[u]; i++)
{
mn = min(mn, -(ret -> begin() -> fst));
(ret -> begin() -> snd)--;
if (ret -> begin() -> snd == 0) {ret -> erase(ret -> begin());}
}
return ret;
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i < n; i++)
{
int v; cin >> v;
adj[v - 1].push_back(i);
adj[i].push_back(v - 1);
}
for (int i = 0; i < k; i++)
{
int x; cin >> x; spc[x - 1] = 1;
}
if (spc[0]) {cout << "1\n";}
else
{
dfs(0, -1); dfs2(0 ,-1, 0);
cout << mn + 1 << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
6528 KB |
Output is correct |
4 |
Correct |
7 ms |
6656 KB |
Output is correct |
5 |
Correct |
6 ms |
6656 KB |
Output is correct |
6 |
Correct |
136 ms |
72192 KB |
Output is correct |
7 |
Correct |
134 ms |
72172 KB |
Output is correct |
8 |
Correct |
6 ms |
6656 KB |
Output is correct |
9 |
Correct |
159 ms |
67432 KB |
Output is correct |
10 |
Correct |
154 ms |
67432 KB |
Output is correct |
11 |
Correct |
160 ms |
69900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
6 ms |
6272 KB |
Output is correct |
3 |
Correct |
5 ms |
6272 KB |
Output is correct |
4 |
Correct |
5 ms |
6068 KB |
Output is correct |
5 |
Correct |
6 ms |
6272 KB |
Output is correct |
6 |
Correct |
5 ms |
6016 KB |
Output is correct |
7 |
Correct |
136 ms |
72192 KB |
Output is correct |
8 |
Correct |
134 ms |
72172 KB |
Output is correct |
9 |
Correct |
6 ms |
6656 KB |
Output is correct |
10 |
Correct |
109 ms |
54508 KB |
Output is correct |
11 |
Correct |
109 ms |
54512 KB |
Output is correct |
12 |
Correct |
146 ms |
43768 KB |
Output is correct |
13 |
Correct |
115 ms |
59620 KB |
Output is correct |
14 |
Correct |
87 ms |
44408 KB |
Output is correct |
15 |
Correct |
154 ms |
43768 KB |
Output is correct |
16 |
Correct |
116 ms |
59620 KB |
Output is correct |
17 |
Correct |
184 ms |
45432 KB |
Output is correct |
18 |
Correct |
137 ms |
62952 KB |
Output is correct |
19 |
Correct |
161 ms |
50808 KB |
Output is correct |
20 |
Correct |
130 ms |
49528 KB |
Output is correct |
21 |
Correct |
129 ms |
49656 KB |
Output is correct |
22 |
Correct |
133 ms |
49656 KB |
Output is correct |
23 |
Correct |
116 ms |
48632 KB |
Output is correct |
24 |
Correct |
119 ms |
49400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
6272 KB |
Output is correct |
7 |
Correct |
5 ms |
6272 KB |
Output is correct |
8 |
Correct |
7 ms |
6272 KB |
Output is correct |
9 |
Correct |
5 ms |
6016 KB |
Output is correct |
10 |
Correct |
6 ms |
6016 KB |
Output is correct |
11 |
Correct |
6 ms |
6400 KB |
Output is correct |
12 |
Correct |
7 ms |
5888 KB |
Output is correct |
13 |
Correct |
7 ms |
6272 KB |
Output is correct |
14 |
Correct |
6 ms |
6016 KB |
Output is correct |
15 |
Correct |
7 ms |
6400 KB |
Output is correct |
16 |
Correct |
7 ms |
6400 KB |
Output is correct |
17 |
Correct |
6 ms |
6144 KB |
Output is correct |
18 |
Correct |
7 ms |
6272 KB |
Output is correct |
19 |
Correct |
7 ms |
6016 KB |
Output is correct |
20 |
Correct |
6 ms |
5888 KB |
Output is correct |
21 |
Correct |
5 ms |
5888 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
3 ms |
4992 KB |
Output is correct |
24 |
Correct |
7 ms |
6528 KB |
Output is correct |
25 |
Correct |
7 ms |
6656 KB |
Output is correct |
26 |
Correct |
6 ms |
6656 KB |
Output is correct |
27 |
Correct |
3 ms |
4992 KB |
Output is correct |
28 |
Correct |
6 ms |
6272 KB |
Output is correct |
29 |
Correct |
5 ms |
6272 KB |
Output is correct |
30 |
Correct |
5 ms |
6068 KB |
Output is correct |
31 |
Correct |
6 ms |
6272 KB |
Output is correct |
32 |
Correct |
5 ms |
6016 KB |
Output is correct |
33 |
Correct |
6 ms |
6656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
6272 KB |
Output is correct |
7 |
Correct |
5 ms |
6272 KB |
Output is correct |
8 |
Correct |
7 ms |
6272 KB |
Output is correct |
9 |
Correct |
5 ms |
6016 KB |
Output is correct |
10 |
Correct |
6 ms |
6016 KB |
Output is correct |
11 |
Correct |
6 ms |
6400 KB |
Output is correct |
12 |
Correct |
7 ms |
5888 KB |
Output is correct |
13 |
Correct |
7 ms |
6272 KB |
Output is correct |
14 |
Correct |
6 ms |
6016 KB |
Output is correct |
15 |
Correct |
7 ms |
6400 KB |
Output is correct |
16 |
Correct |
7 ms |
6400 KB |
Output is correct |
17 |
Correct |
6 ms |
6144 KB |
Output is correct |
18 |
Correct |
7 ms |
6272 KB |
Output is correct |
19 |
Correct |
7 ms |
6016 KB |
Output is correct |
20 |
Correct |
6 ms |
5888 KB |
Output is correct |
21 |
Correct |
5 ms |
5888 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
3 ms |
4992 KB |
Output is correct |
24 |
Correct |
7 ms |
6528 KB |
Output is correct |
25 |
Correct |
7 ms |
6656 KB |
Output is correct |
26 |
Correct |
6 ms |
6656 KB |
Output is correct |
27 |
Correct |
3 ms |
4992 KB |
Output is correct |
28 |
Correct |
6 ms |
6272 KB |
Output is correct |
29 |
Correct |
5 ms |
6272 KB |
Output is correct |
30 |
Correct |
5 ms |
6068 KB |
Output is correct |
31 |
Correct |
6 ms |
6272 KB |
Output is correct |
32 |
Correct |
5 ms |
6016 KB |
Output is correct |
33 |
Correct |
136 ms |
72192 KB |
Output is correct |
34 |
Correct |
134 ms |
72172 KB |
Output is correct |
35 |
Correct |
6 ms |
6656 KB |
Output is correct |
36 |
Correct |
109 ms |
54508 KB |
Output is correct |
37 |
Correct |
109 ms |
54512 KB |
Output is correct |
38 |
Correct |
146 ms |
43768 KB |
Output is correct |
39 |
Correct |
115 ms |
59620 KB |
Output is correct |
40 |
Correct |
87 ms |
44408 KB |
Output is correct |
41 |
Correct |
154 ms |
43768 KB |
Output is correct |
42 |
Correct |
116 ms |
59620 KB |
Output is correct |
43 |
Correct |
184 ms |
45432 KB |
Output is correct |
44 |
Correct |
137 ms |
62952 KB |
Output is correct |
45 |
Correct |
161 ms |
50808 KB |
Output is correct |
46 |
Correct |
130 ms |
49528 KB |
Output is correct |
47 |
Correct |
129 ms |
49656 KB |
Output is correct |
48 |
Correct |
133 ms |
49656 KB |
Output is correct |
49 |
Correct |
116 ms |
48632 KB |
Output is correct |
50 |
Correct |
119 ms |
49400 KB |
Output is correct |
51 |
Correct |
159 ms |
67432 KB |
Output is correct |
52 |
Correct |
154 ms |
67432 KB |
Output is correct |
53 |
Correct |
160 ms |
69900 KB |
Output is correct |
54 |
Correct |
114 ms |
54560 KB |
Output is correct |
55 |
Correct |
116 ms |
54508 KB |
Output is correct |
56 |
Correct |
113 ms |
55920 KB |
Output is correct |
57 |
Correct |
101 ms |
44408 KB |
Output is correct |
58 |
Correct |
178 ms |
43796 KB |
Output is correct |
59 |
Correct |
128 ms |
51060 KB |
Output is correct |
60 |
Correct |
162 ms |
41852 KB |
Output is correct |
61 |
Correct |
140 ms |
55792 KB |
Output is correct |
62 |
Correct |
196 ms |
45176 KB |
Output is correct |
63 |
Correct |
146 ms |
60136 KB |
Output is correct |
64 |
Correct |
140 ms |
49144 KB |
Output is correct |
65 |
Correct |
125 ms |
48604 KB |
Output is correct |
66 |
Correct |
128 ms |
45944 KB |
Output is correct |
67 |
Correct |
128 ms |
47480 KB |
Output is correct |
68 |
Correct |
115 ms |
48120 KB |
Output is correct |
69 |
Correct |
133 ms |
43400 KB |
Output is correct |
70 |
Correct |
57 ms |
25592 KB |
Output is correct |
71 |
Correct |
115 ms |
48372 KB |
Output is correct |