#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
void setIO(string name = "")
{
cin.tie(0)->sync_with_stdio(0); // see /general/fast-io
if (sz(name))
{
freopen((name + ".in").c_str(), "r", stdin); // see /general/input-output
freopen((name + ".out").c_str(), "w", stdout);
}
}
/*----------------------------------*/
const int MAXN = 2e5 + 10, maxLog = 32;
int n, m, dep[MAXN] {0}, par[MAXN][maxLog] = {0}, best[MAXN];
vector<int> adj[MAXN];
void dfs(int v, int u) {
dep[v] = dep[u] + 1;
par[v][0] = u;
for (auto i : adj[v]) {
if (i == u) continue;
dfs(i, v);
}
}
int lift(int p, int x) {
for (int i = 0; i < maxLog; ++i) {
if (x & (1 << i)) {
p = par[p][i];
}
}
return p;
}
bool pos = true;
int possible(int v, int u, int val) {
int cnt = 0;
for (auto i : adj[v]) {
if (i == u) continue;
cnt += possible(i, v, val);
}
if (dep[v] >= val - 1) cnt++;
if (cnt < best[v]) pos = false;
cnt -= best[v];
return cnt;
}
int solve() {
int l = 0, r = n;
while (l < r - 1) {
int m = l + (r - l) / 2;
pos = true;
possible(1, 0, m);
if (pos) l = m;
else r = m - 1;
}
pos = true;
possible(1, 0, r + 1);
if (pos) return r + 1;
pos = true;
possible(1, 0, r);
if (pos) return r;
return r - 1;
}
int main()
{
setIO("");
cin >> n >> m;
for (int i = 2; i <= n; ++i) {
int a;
cin >> a;
adj[a].push_back(i);
adj[i].push_back(a);
}
dep[0] = -1;
par[0][0] = 0;
dfs(1, 0);
// cerr << "a\n";
for (int i = 1; i < maxLog; ++i) {
for (int x = 1; x <= n; ++x) {
par[x][i] = par[par[x][i - 1]][i - 1];
}
}
// cerr << "a\n";
for (int i = 0; i < m; ++i) {
int a;
cin >> a;
best[lift(a, (dep[a] - 1) / 2)]++;
}
cout << solve();
}
Compilation message
mokesciai.cpp: In function 'void setIO(std::string)':
mokesciai.cpp:11:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | freopen((name + ".in").c_str(), "r", stdin); // see /general/input-output
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mokesciai.cpp:12:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
5 ms |
6148 KB |
Output is correct |
4 |
Correct |
6 ms |
6196 KB |
Output is correct |
5 |
Correct |
7 ms |
6264 KB |
Output is correct |
6 |
Correct |
202 ms |
54092 KB |
Output is correct |
7 |
Correct |
181 ms |
53904 KB |
Output is correct |
8 |
Correct |
5 ms |
6188 KB |
Output is correct |
9 |
Correct |
323 ms |
55388 KB |
Output is correct |
10 |
Correct |
365 ms |
55668 KB |
Output is correct |
11 |
Correct |
243 ms |
54712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
54092 KB |
Output is correct |
2 |
Correct |
181 ms |
53904 KB |
Output is correct |
3 |
Correct |
5 ms |
6188 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
6008 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
4 ms |
5844 KB |
Output is correct |
8 |
Correct |
5 ms |
5928 KB |
Output is correct |
9 |
Correct |
4 ms |
5804 KB |
Output is correct |
10 |
Correct |
158 ms |
44976 KB |
Output is correct |
11 |
Correct |
149 ms |
44868 KB |
Output is correct |
12 |
Correct |
325 ms |
38424 KB |
Output is correct |
13 |
Correct |
199 ms |
46156 KB |
Output is correct |
14 |
Correct |
147 ms |
38140 KB |
Output is correct |
15 |
Correct |
340 ms |
38604 KB |
Output is correct |
16 |
Correct |
191 ms |
46292 KB |
Output is correct |
17 |
Correct |
429 ms |
38496 KB |
Output is correct |
18 |
Correct |
187 ms |
47820 KB |
Output is correct |
19 |
Correct |
177 ms |
39360 KB |
Output is correct |
20 |
Correct |
211 ms |
38708 KB |
Output is correct |
21 |
Correct |
196 ms |
38592 KB |
Output is correct |
22 |
Correct |
201 ms |
38572 KB |
Output is correct |
23 |
Correct |
245 ms |
38464 KB |
Output is correct |
24 |
Correct |
188 ms |
38344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
5 ms |
6148 KB |
Output is correct |
4 |
Correct |
6 ms |
6196 KB |
Output is correct |
5 |
Correct |
7 ms |
6264 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
6008 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5844 KB |
Output is correct |
10 |
Correct |
5 ms |
5928 KB |
Output is correct |
11 |
Correct |
4 ms |
5804 KB |
Output is correct |
12 |
Correct |
5 ms |
6188 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5972 KB |
Output is correct |
19 |
Correct |
4 ms |
5972 KB |
Output is correct |
20 |
Correct |
5 ms |
5972 KB |
Output is correct |
21 |
Correct |
5 ms |
5844 KB |
Output is correct |
22 |
Correct |
6 ms |
5844 KB |
Output is correct |
23 |
Correct |
5 ms |
5972 KB |
Output is correct |
24 |
Correct |
6 ms |
5844 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
5 ms |
5816 KB |
Output is correct |
27 |
Correct |
5 ms |
6100 KB |
Output is correct |
28 |
Correct |
5 ms |
6148 KB |
Output is correct |
29 |
Correct |
5 ms |
5848 KB |
Output is correct |
30 |
Correct |
6 ms |
5972 KB |
Output is correct |
31 |
Correct |
5 ms |
5844 KB |
Output is correct |
32 |
Correct |
5 ms |
5676 KB |
Output is correct |
33 |
Correct |
4 ms |
5676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
4 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
5972 KB |
Output is correct |
9 |
Correct |
5 ms |
5844 KB |
Output is correct |
10 |
Correct |
6 ms |
5844 KB |
Output is correct |
11 |
Correct |
5 ms |
5972 KB |
Output is correct |
12 |
Correct |
6 ms |
5844 KB |
Output is correct |
13 |
Correct |
5 ms |
5972 KB |
Output is correct |
14 |
Correct |
5 ms |
5816 KB |
Output is correct |
15 |
Correct |
5 ms |
6100 KB |
Output is correct |
16 |
Correct |
5 ms |
6148 KB |
Output is correct |
17 |
Correct |
5 ms |
5848 KB |
Output is correct |
18 |
Correct |
6 ms |
5972 KB |
Output is correct |
19 |
Correct |
5 ms |
5844 KB |
Output is correct |
20 |
Correct |
5 ms |
5676 KB |
Output is correct |
21 |
Correct |
4 ms |
5676 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
5 ms |
6148 KB |
Output is correct |
25 |
Correct |
6 ms |
6196 KB |
Output is correct |
26 |
Correct |
7 ms |
6264 KB |
Output is correct |
27 |
Correct |
2 ms |
4948 KB |
Output is correct |
28 |
Correct |
4 ms |
6008 KB |
Output is correct |
29 |
Correct |
4 ms |
5972 KB |
Output is correct |
30 |
Correct |
4 ms |
5844 KB |
Output is correct |
31 |
Correct |
5 ms |
5928 KB |
Output is correct |
32 |
Correct |
4 ms |
5804 KB |
Output is correct |
33 |
Correct |
202 ms |
54092 KB |
Output is correct |
34 |
Correct |
181 ms |
53904 KB |
Output is correct |
35 |
Correct |
5 ms |
6188 KB |
Output is correct |
36 |
Correct |
158 ms |
44976 KB |
Output is correct |
37 |
Correct |
149 ms |
44868 KB |
Output is correct |
38 |
Correct |
325 ms |
38424 KB |
Output is correct |
39 |
Correct |
199 ms |
46156 KB |
Output is correct |
40 |
Correct |
147 ms |
38140 KB |
Output is correct |
41 |
Correct |
340 ms |
38604 KB |
Output is correct |
42 |
Correct |
191 ms |
46292 KB |
Output is correct |
43 |
Correct |
429 ms |
38496 KB |
Output is correct |
44 |
Correct |
187 ms |
47820 KB |
Output is correct |
45 |
Correct |
177 ms |
39360 KB |
Output is correct |
46 |
Correct |
211 ms |
38708 KB |
Output is correct |
47 |
Correct |
196 ms |
38592 KB |
Output is correct |
48 |
Correct |
201 ms |
38572 KB |
Output is correct |
49 |
Correct |
245 ms |
38464 KB |
Output is correct |
50 |
Correct |
188 ms |
38344 KB |
Output is correct |
51 |
Correct |
323 ms |
55388 KB |
Output is correct |
52 |
Correct |
365 ms |
55668 KB |
Output is correct |
53 |
Correct |
243 ms |
54712 KB |
Output is correct |
54 |
Correct |
152 ms |
45152 KB |
Output is correct |
55 |
Correct |
149 ms |
45188 KB |
Output is correct |
56 |
Correct |
215 ms |
47168 KB |
Output is correct |
57 |
Correct |
150 ms |
38724 KB |
Output is correct |
58 |
Correct |
269 ms |
38348 KB |
Output is correct |
59 |
Correct |
220 ms |
42576 KB |
Output is correct |
60 |
Correct |
332 ms |
39584 KB |
Output is correct |
61 |
Correct |
285 ms |
47720 KB |
Output is correct |
62 |
Correct |
437 ms |
39476 KB |
Output is correct |
63 |
Correct |
236 ms |
48460 KB |
Output is correct |
64 |
Correct |
216 ms |
39964 KB |
Output is correct |
65 |
Correct |
227 ms |
39056 KB |
Output is correct |
66 |
Correct |
233 ms |
39408 KB |
Output is correct |
67 |
Correct |
241 ms |
39348 KB |
Output is correct |
68 |
Correct |
289 ms |
38976 KB |
Output is correct |
69 |
Correct |
263 ms |
39520 KB |
Output is correct |
70 |
Correct |
91 ms |
22792 KB |
Output is correct |
71 |
Correct |
172 ms |
42316 KB |
Output is correct |