#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];
vector<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 = nullptr, *rr;
for (auto v : adj[u])
{
if (v != p)
{
rr = dfs2(v, u, h + 1);
if (ret == nullptr || rr -> size() > ret -> size()) {swap(rr, ret);}
if (rr != nullptr) for (auto v : *rr) {(*ret)[v.fst] += v.snd;}
}
}
if (ret == nullptr) {ret = new map<int, int>;}
(*ret)[-h]++;
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 |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
6016 KB |
Output is correct |
4 |
Correct |
6 ms |
6016 KB |
Output is correct |
5 |
Correct |
6 ms |
6016 KB |
Output is correct |
6 |
Correct |
111 ms |
47084 KB |
Output is correct |
7 |
Correct |
110 ms |
47088 KB |
Output is correct |
8 |
Correct |
5 ms |
6144 KB |
Output is correct |
9 |
Correct |
125 ms |
42532 KB |
Output is correct |
10 |
Correct |
132 ms |
42472 KB |
Output is correct |
11 |
Correct |
126 ms |
44724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
5 ms |
5888 KB |
Output is correct |
3 |
Correct |
5 ms |
5888 KB |
Output is correct |
4 |
Correct |
5 ms |
5632 KB |
Output is correct |
5 |
Correct |
5 ms |
5760 KB |
Output is correct |
6 |
Correct |
5 ms |
5632 KB |
Output is correct |
7 |
Correct |
111 ms |
47084 KB |
Output is correct |
8 |
Correct |
110 ms |
47088 KB |
Output is correct |
9 |
Correct |
5 ms |
6144 KB |
Output is correct |
10 |
Correct |
89 ms |
36716 KB |
Output is correct |
11 |
Correct |
90 ms |
36716 KB |
Output is correct |
12 |
Correct |
116 ms |
30444 KB |
Output is correct |
13 |
Correct |
99 ms |
38000 KB |
Output is correct |
14 |
Correct |
76 ms |
27128 KB |
Output is correct |
15 |
Correct |
125 ms |
30584 KB |
Output is correct |
16 |
Correct |
101 ms |
37872 KB |
Output is correct |
17 |
Correct |
166 ms |
27768 KB |
Output is correct |
18 |
Correct |
112 ms |
36664 KB |
Output is correct |
19 |
Correct |
103 ms |
22776 KB |
Output is correct |
20 |
Correct |
93 ms |
21496 KB |
Output is correct |
21 |
Correct |
92 ms |
21496 KB |
Output is correct |
22 |
Correct |
93 ms |
21420 KB |
Output is correct |
23 |
Correct |
81 ms |
21624 KB |
Output is correct |
24 |
Correct |
88 ms |
21368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5888 KB |
Output is correct |
7 |
Correct |
5 ms |
5888 KB |
Output is correct |
8 |
Correct |
5 ms |
5888 KB |
Output is correct |
9 |
Correct |
6 ms |
5632 KB |
Output is correct |
10 |
Correct |
5 ms |
5632 KB |
Output is correct |
11 |
Correct |
7 ms |
5888 KB |
Output is correct |
12 |
Correct |
6 ms |
5632 KB |
Output is correct |
13 |
Correct |
6 ms |
5632 KB |
Output is correct |
14 |
Correct |
6 ms |
5632 KB |
Output is correct |
15 |
Correct |
6 ms |
5760 KB |
Output is correct |
16 |
Correct |
5 ms |
5760 KB |
Output is correct |
17 |
Correct |
6 ms |
5376 KB |
Output is correct |
18 |
Correct |
6 ms |
5632 KB |
Output is correct |
19 |
Correct |
5 ms |
5376 KB |
Output is correct |
20 |
Correct |
5 ms |
5504 KB |
Output is correct |
21 |
Correct |
5 ms |
5504 KB |
Output is correct |
22 |
Correct |
3 ms |
4992 KB |
Output is correct |
23 |
Correct |
3 ms |
4992 KB |
Output is correct |
24 |
Correct |
6 ms |
6016 KB |
Output is correct |
25 |
Correct |
6 ms |
6016 KB |
Output is correct |
26 |
Correct |
6 ms |
6016 KB |
Output is correct |
27 |
Correct |
3 ms |
4992 KB |
Output is correct |
28 |
Correct |
5 ms |
5888 KB |
Output is correct |
29 |
Correct |
5 ms |
5888 KB |
Output is correct |
30 |
Correct |
5 ms |
5632 KB |
Output is correct |
31 |
Correct |
5 ms |
5760 KB |
Output is correct |
32 |
Correct |
5 ms |
5632 KB |
Output is correct |
33 |
Correct |
5 ms |
6144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5888 KB |
Output is correct |
7 |
Correct |
5 ms |
5888 KB |
Output is correct |
8 |
Correct |
5 ms |
5888 KB |
Output is correct |
9 |
Correct |
6 ms |
5632 KB |
Output is correct |
10 |
Correct |
5 ms |
5632 KB |
Output is correct |
11 |
Correct |
7 ms |
5888 KB |
Output is correct |
12 |
Correct |
6 ms |
5632 KB |
Output is correct |
13 |
Correct |
6 ms |
5632 KB |
Output is correct |
14 |
Correct |
6 ms |
5632 KB |
Output is correct |
15 |
Correct |
6 ms |
5760 KB |
Output is correct |
16 |
Correct |
5 ms |
5760 KB |
Output is correct |
17 |
Correct |
6 ms |
5376 KB |
Output is correct |
18 |
Correct |
6 ms |
5632 KB |
Output is correct |
19 |
Correct |
5 ms |
5376 KB |
Output is correct |
20 |
Correct |
5 ms |
5504 KB |
Output is correct |
21 |
Correct |
5 ms |
5504 KB |
Output is correct |
22 |
Correct |
3 ms |
4992 KB |
Output is correct |
23 |
Correct |
3 ms |
4992 KB |
Output is correct |
24 |
Correct |
6 ms |
6016 KB |
Output is correct |
25 |
Correct |
6 ms |
6016 KB |
Output is correct |
26 |
Correct |
6 ms |
6016 KB |
Output is correct |
27 |
Correct |
3 ms |
4992 KB |
Output is correct |
28 |
Correct |
5 ms |
5888 KB |
Output is correct |
29 |
Correct |
5 ms |
5888 KB |
Output is correct |
30 |
Correct |
5 ms |
5632 KB |
Output is correct |
31 |
Correct |
5 ms |
5760 KB |
Output is correct |
32 |
Correct |
5 ms |
5632 KB |
Output is correct |
33 |
Correct |
111 ms |
47084 KB |
Output is correct |
34 |
Correct |
110 ms |
47088 KB |
Output is correct |
35 |
Correct |
5 ms |
6144 KB |
Output is correct |
36 |
Correct |
89 ms |
36716 KB |
Output is correct |
37 |
Correct |
90 ms |
36716 KB |
Output is correct |
38 |
Correct |
116 ms |
30444 KB |
Output is correct |
39 |
Correct |
99 ms |
38000 KB |
Output is correct |
40 |
Correct |
76 ms |
27128 KB |
Output is correct |
41 |
Correct |
125 ms |
30584 KB |
Output is correct |
42 |
Correct |
101 ms |
37872 KB |
Output is correct |
43 |
Correct |
166 ms |
27768 KB |
Output is correct |
44 |
Correct |
112 ms |
36664 KB |
Output is correct |
45 |
Correct |
103 ms |
22776 KB |
Output is correct |
46 |
Correct |
93 ms |
21496 KB |
Output is correct |
47 |
Correct |
92 ms |
21496 KB |
Output is correct |
48 |
Correct |
93 ms |
21420 KB |
Output is correct |
49 |
Correct |
81 ms |
21624 KB |
Output is correct |
50 |
Correct |
88 ms |
21368 KB |
Output is correct |
51 |
Correct |
125 ms |
42532 KB |
Output is correct |
52 |
Correct |
132 ms |
42472 KB |
Output is correct |
53 |
Correct |
126 ms |
44724 KB |
Output is correct |
54 |
Correct |
95 ms |
36844 KB |
Output is correct |
55 |
Correct |
97 ms |
36716 KB |
Output is correct |
56 |
Correct |
98 ms |
40692 KB |
Output is correct |
57 |
Correct |
91 ms |
27128 KB |
Output is correct |
58 |
Correct |
117 ms |
30504 KB |
Output is correct |
59 |
Correct |
106 ms |
29800 KB |
Output is correct |
60 |
Correct |
144 ms |
28524 KB |
Output is correct |
61 |
Correct |
123 ms |
34284 KB |
Output is correct |
62 |
Correct |
190 ms |
27516 KB |
Output is correct |
63 |
Correct |
118 ms |
33904 KB |
Output is correct |
64 |
Correct |
106 ms |
21240 KB |
Output is correct |
65 |
Correct |
95 ms |
20344 KB |
Output is correct |
66 |
Correct |
95 ms |
17656 KB |
Output is correct |
67 |
Correct |
99 ms |
19320 KB |
Output is correct |
68 |
Correct |
93 ms |
21136 KB |
Output is correct |
69 |
Correct |
98 ms |
15352 KB |
Output is correct |
70 |
Correct |
55 ms |
16632 KB |
Output is correct |
71 |
Correct |
98 ms |
29556 KB |
Output is correct |