#include <iostream>
#include <set>
#include <vector>
#include <assert.h>
#include <map>
#include <deque>
#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];
deque<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 |
94 ms |
135032 KB |
Output is correct |
2 |
Correct |
95 ms |
135032 KB |
Output is correct |
3 |
Correct |
97 ms |
136468 KB |
Output is correct |
4 |
Correct |
97 ms |
136440 KB |
Output is correct |
5 |
Correct |
97 ms |
136568 KB |
Output is correct |
6 |
Correct |
208 ms |
195820 KB |
Output is correct |
7 |
Correct |
204 ms |
195820 KB |
Output is correct |
8 |
Correct |
122 ms |
136568 KB |
Output is correct |
9 |
Correct |
221 ms |
191084 KB |
Output is correct |
10 |
Correct |
223 ms |
191084 KB |
Output is correct |
11 |
Correct |
234 ms |
193640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
135032 KB |
Output is correct |
2 |
Correct |
96 ms |
135928 KB |
Output is correct |
3 |
Correct |
100 ms |
135928 KB |
Output is correct |
4 |
Correct |
98 ms |
135672 KB |
Output is correct |
5 |
Correct |
98 ms |
135928 KB |
Output is correct |
6 |
Correct |
100 ms |
135708 KB |
Output is correct |
7 |
Correct |
208 ms |
195820 KB |
Output is correct |
8 |
Correct |
204 ms |
195820 KB |
Output is correct |
9 |
Correct |
122 ms |
136568 KB |
Output is correct |
10 |
Correct |
196 ms |
174828 KB |
Output is correct |
11 |
Correct |
198 ms |
174836 KB |
Output is correct |
12 |
Correct |
236 ms |
161720 KB |
Output is correct |
13 |
Correct |
218 ms |
180076 KB |
Output is correct |
14 |
Correct |
177 ms |
161836 KB |
Output is correct |
15 |
Correct |
255 ms |
161680 KB |
Output is correct |
16 |
Correct |
260 ms |
180076 KB |
Output is correct |
17 |
Correct |
275 ms |
162940 KB |
Output is correct |
18 |
Correct |
242 ms |
184176 KB |
Output is correct |
19 |
Correct |
278 ms |
168568 KB |
Output is correct |
20 |
Correct |
229 ms |
167052 KB |
Output is correct |
21 |
Correct |
268 ms |
167160 KB |
Output is correct |
22 |
Correct |
241 ms |
167128 KB |
Output is correct |
23 |
Correct |
210 ms |
166136 KB |
Output is correct |
24 |
Correct |
219 ms |
166776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
134904 KB |
Output is correct |
2 |
Correct |
92 ms |
135032 KB |
Output is correct |
3 |
Correct |
94 ms |
135032 KB |
Output is correct |
4 |
Correct |
93 ms |
134904 KB |
Output is correct |
5 |
Correct |
105 ms |
135032 KB |
Output is correct |
6 |
Correct |
95 ms |
135928 KB |
Output is correct |
7 |
Correct |
97 ms |
135928 KB |
Output is correct |
8 |
Correct |
97 ms |
136048 KB |
Output is correct |
9 |
Correct |
102 ms |
135672 KB |
Output is correct |
10 |
Correct |
100 ms |
135696 KB |
Output is correct |
11 |
Correct |
97 ms |
136056 KB |
Output is correct |
12 |
Correct |
99 ms |
135544 KB |
Output is correct |
13 |
Correct |
97 ms |
135928 KB |
Output is correct |
14 |
Correct |
97 ms |
135800 KB |
Output is correct |
15 |
Correct |
103 ms |
136184 KB |
Output is correct |
16 |
Correct |
105 ms |
136184 KB |
Output is correct |
17 |
Correct |
98 ms |
135800 KB |
Output is correct |
18 |
Correct |
102 ms |
135928 KB |
Output is correct |
19 |
Correct |
97 ms |
135672 KB |
Output is correct |
20 |
Correct |
99 ms |
135800 KB |
Output is correct |
21 |
Correct |
101 ms |
135800 KB |
Output is correct |
22 |
Correct |
94 ms |
135032 KB |
Output is correct |
23 |
Correct |
95 ms |
135032 KB |
Output is correct |
24 |
Correct |
97 ms |
136468 KB |
Output is correct |
25 |
Correct |
97 ms |
136440 KB |
Output is correct |
26 |
Correct |
97 ms |
136568 KB |
Output is correct |
27 |
Correct |
93 ms |
135032 KB |
Output is correct |
28 |
Correct |
96 ms |
135928 KB |
Output is correct |
29 |
Correct |
100 ms |
135928 KB |
Output is correct |
30 |
Correct |
98 ms |
135672 KB |
Output is correct |
31 |
Correct |
98 ms |
135928 KB |
Output is correct |
32 |
Correct |
100 ms |
135708 KB |
Output is correct |
33 |
Correct |
122 ms |
136568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
134904 KB |
Output is correct |
2 |
Correct |
92 ms |
135032 KB |
Output is correct |
3 |
Correct |
94 ms |
135032 KB |
Output is correct |
4 |
Correct |
93 ms |
134904 KB |
Output is correct |
5 |
Correct |
105 ms |
135032 KB |
Output is correct |
6 |
Correct |
95 ms |
135928 KB |
Output is correct |
7 |
Correct |
97 ms |
135928 KB |
Output is correct |
8 |
Correct |
97 ms |
136048 KB |
Output is correct |
9 |
Correct |
102 ms |
135672 KB |
Output is correct |
10 |
Correct |
100 ms |
135696 KB |
Output is correct |
11 |
Correct |
97 ms |
136056 KB |
Output is correct |
12 |
Correct |
99 ms |
135544 KB |
Output is correct |
13 |
Correct |
97 ms |
135928 KB |
Output is correct |
14 |
Correct |
97 ms |
135800 KB |
Output is correct |
15 |
Correct |
103 ms |
136184 KB |
Output is correct |
16 |
Correct |
105 ms |
136184 KB |
Output is correct |
17 |
Correct |
98 ms |
135800 KB |
Output is correct |
18 |
Correct |
102 ms |
135928 KB |
Output is correct |
19 |
Correct |
97 ms |
135672 KB |
Output is correct |
20 |
Correct |
99 ms |
135800 KB |
Output is correct |
21 |
Correct |
101 ms |
135800 KB |
Output is correct |
22 |
Correct |
94 ms |
135032 KB |
Output is correct |
23 |
Correct |
95 ms |
135032 KB |
Output is correct |
24 |
Correct |
97 ms |
136468 KB |
Output is correct |
25 |
Correct |
97 ms |
136440 KB |
Output is correct |
26 |
Correct |
97 ms |
136568 KB |
Output is correct |
27 |
Correct |
93 ms |
135032 KB |
Output is correct |
28 |
Correct |
96 ms |
135928 KB |
Output is correct |
29 |
Correct |
100 ms |
135928 KB |
Output is correct |
30 |
Correct |
98 ms |
135672 KB |
Output is correct |
31 |
Correct |
98 ms |
135928 KB |
Output is correct |
32 |
Correct |
100 ms |
135708 KB |
Output is correct |
33 |
Correct |
208 ms |
195820 KB |
Output is correct |
34 |
Correct |
204 ms |
195820 KB |
Output is correct |
35 |
Correct |
122 ms |
136568 KB |
Output is correct |
36 |
Correct |
196 ms |
174828 KB |
Output is correct |
37 |
Correct |
198 ms |
174836 KB |
Output is correct |
38 |
Correct |
236 ms |
161720 KB |
Output is correct |
39 |
Correct |
218 ms |
180076 KB |
Output is correct |
40 |
Correct |
177 ms |
161836 KB |
Output is correct |
41 |
Correct |
255 ms |
161680 KB |
Output is correct |
42 |
Correct |
260 ms |
180076 KB |
Output is correct |
43 |
Correct |
275 ms |
162940 KB |
Output is correct |
44 |
Correct |
242 ms |
184176 KB |
Output is correct |
45 |
Correct |
278 ms |
168568 KB |
Output is correct |
46 |
Correct |
229 ms |
167052 KB |
Output is correct |
47 |
Correct |
268 ms |
167160 KB |
Output is correct |
48 |
Correct |
241 ms |
167128 KB |
Output is correct |
49 |
Correct |
210 ms |
166136 KB |
Output is correct |
50 |
Correct |
219 ms |
166776 KB |
Output is correct |
51 |
Correct |
221 ms |
191084 KB |
Output is correct |
52 |
Correct |
223 ms |
191084 KB |
Output is correct |
53 |
Correct |
234 ms |
193640 KB |
Output is correct |
54 |
Correct |
206 ms |
174836 KB |
Output is correct |
55 |
Correct |
207 ms |
174836 KB |
Output is correct |
56 |
Correct |
205 ms |
176884 KB |
Output is correct |
57 |
Correct |
219 ms |
161716 KB |
Output is correct |
58 |
Correct |
229 ms |
161656 KB |
Output is correct |
59 |
Correct |
271 ms |
169832 KB |
Output is correct |
60 |
Correct |
260 ms |
159608 KB |
Output is correct |
61 |
Correct |
281 ms |
176368 KB |
Output is correct |
62 |
Correct |
346 ms |
162680 KB |
Output is correct |
63 |
Correct |
250 ms |
181360 KB |
Output is correct |
64 |
Correct |
285 ms |
167160 KB |
Output is correct |
65 |
Correct |
235 ms |
166008 KB |
Output is correct |
66 |
Correct |
272 ms |
163320 KB |
Output is correct |
67 |
Correct |
274 ms |
164984 KB |
Output is correct |
68 |
Correct |
269 ms |
165624 KB |
Output is correct |
69 |
Correct |
279 ms |
160760 KB |
Output is correct |
70 |
Correct |
174 ms |
149116 KB |
Output is correct |
71 |
Correct |
214 ms |
169456 KB |
Output is correct |