#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int LOGN = 19;
vector<int> v[MAXN];
int n, m, tick;
int dsc[MAXN], fin[MAXN], lvl[MAXN], dad[MAXN][LOGN];
inline pair<int, int> maxp(const pair<int, int> &a, const pair<int, int> &b) {
if (a > b) return a;
return b;
}
struct Tournament {
int offset;
vector<pair<int, int>> t;
Tournament(int sz) {
for (offset = 1; offset <= sz; offset *= 2);
t.resize(2 * offset);
}
void ins(int i, int val) {
i += offset; t[i] = {val, i - offset};
for (i /= 2; i > 0; i /= 2)
t[i] = maxp(t[2 * i], t[2 * i + 1]);
}
pair<int, int> max(int node, int from, int to, int lo, int hi) {
if (to <= lo || hi <= from) return {-1, -1};
if (from >= lo && to <= hi) return t[node];
return maxp(max(2 * node, from, (from + to) / 2, lo, hi),
max(2 * node + 1, (from + to) / 2, to, lo, hi));
}
pair<int, int> max(int lo, int hi) {
return max(1, 0, offset, lo, hi);
}
};
inline void init() {
for (int j = 1; j < LOGN; ++j)
for (int i = 0; i < n; ++i)
dad[i][j] = dad[dad[i][j - 1]][j - 1];
}
void dfs(int node, int l) {
++tick;
dsc[node] = tick;
lvl[node] = l;
for (int nxt : v[node]) {
if (nxt == dad[node][0]) continue;
dfs(nxt, l + 1);
}
fin[node] = tick;
}
int up(int node, int k) {
for (int j = LOGN - 1; j >= 0; --j) {
if ((1 << j) <= k) {
node = dad[node][j];
k -= 1 << j;
}
}
return node;
}
int main(void) {
scanf("%d%d", &n, &m);
for (int i = 1; i < n; ++i) {
scanf("%d", &dad[i][0]);
--dad[i][0];
v[dad[i][0]].push_back(i);
}
init();
dfs(0, 0);
vector<pair<int, int>> I;
for (int i = 0; i < m; ++i) {
int x;
scanf("%d", &x); --x;
x = up(x, (lvl[x] - 1) / 2);
I.emplace_back(dsc[x], fin[x]);
}
sort(I.begin(), I.end(),
[&](const pair<int, int> &A, const pair<int, int> &B) {
return A.second - A.first < B.second - B.first;
});
Tournament T(n + 10);
for (int i = 0; i < n; ++i)
T.ins(dsc[i], lvl[i]);
int sol = n + 10;
for (const auto &p : I) {
auto mx = T.max(p.first, p.second + 1);
sol = min(sol, mx.first);
T.ins(mx.second, 0);
}
printf("%d\n", sol + 1);
return 0;
}
Compilation message
mokesciai.cpp: In function 'int main()':
mokesciai.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
mokesciai.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d", &dad[i][0]);
| ~~~~~^~~~~~~~~~~~~~~~~~
mokesciai.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d", &x); --x;
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
6 ms |
6044 KB |
Output is correct |
4 |
Correct |
6 ms |
6100 KB |
Output is correct |
5 |
Correct |
5 ms |
6088 KB |
Output is correct |
6 |
Correct |
96 ms |
46372 KB |
Output is correct |
7 |
Correct |
95 ms |
46328 KB |
Output is correct |
8 |
Correct |
4 ms |
6044 KB |
Output is correct |
9 |
Correct |
266 ms |
48456 KB |
Output is correct |
10 |
Correct |
299 ms |
49324 KB |
Output is correct |
11 |
Correct |
211 ms |
47828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
46372 KB |
Output is correct |
2 |
Correct |
95 ms |
46328 KB |
Output is correct |
3 |
Correct |
4 ms |
6044 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
5828 KB |
Output is correct |
6 |
Correct |
4 ms |
5788 KB |
Output is correct |
7 |
Correct |
4 ms |
5660 KB |
Output is correct |
8 |
Correct |
4 ms |
5792 KB |
Output is correct |
9 |
Correct |
4 ms |
5664 KB |
Output is correct |
10 |
Correct |
87 ms |
36808 KB |
Output is correct |
11 |
Correct |
93 ms |
36780 KB |
Output is correct |
12 |
Correct |
103 ms |
29552 KB |
Output is correct |
13 |
Correct |
103 ms |
38192 KB |
Output is correct |
14 |
Correct |
87 ms |
30528 KB |
Output is correct |
15 |
Correct |
116 ms |
29956 KB |
Output is correct |
16 |
Correct |
98 ms |
38240 KB |
Output is correct |
17 |
Correct |
145 ms |
30716 KB |
Output is correct |
18 |
Correct |
100 ms |
41336 KB |
Output is correct |
19 |
Correct |
101 ms |
34540 KB |
Output is correct |
20 |
Correct |
116 ms |
34016 KB |
Output is correct |
21 |
Correct |
110 ms |
33984 KB |
Output is correct |
22 |
Correct |
114 ms |
34028 KB |
Output is correct |
23 |
Correct |
99 ms |
33468 KB |
Output is correct |
24 |
Correct |
104 ms |
33736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
6 ms |
6044 KB |
Output is correct |
4 |
Correct |
6 ms |
6100 KB |
Output is correct |
5 |
Correct |
5 ms |
6088 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
5828 KB |
Output is correct |
8 |
Correct |
4 ms |
5788 KB |
Output is correct |
9 |
Correct |
4 ms |
5660 KB |
Output is correct |
10 |
Correct |
4 ms |
5792 KB |
Output is correct |
11 |
Correct |
4 ms |
5664 KB |
Output is correct |
12 |
Correct |
4 ms |
6044 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
5 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
5836 KB |
Output is correct |
20 |
Correct |
5 ms |
5912 KB |
Output is correct |
21 |
Correct |
5 ms |
5716 KB |
Output is correct |
22 |
Correct |
4 ms |
5684 KB |
Output is correct |
23 |
Correct |
6 ms |
5788 KB |
Output is correct |
24 |
Correct |
5 ms |
5716 KB |
Output is correct |
25 |
Correct |
6 ms |
5844 KB |
Output is correct |
26 |
Correct |
6 ms |
5660 KB |
Output is correct |
27 |
Correct |
5 ms |
5972 KB |
Output is correct |
28 |
Correct |
5 ms |
5972 KB |
Output is correct |
29 |
Correct |
5 ms |
5716 KB |
Output is correct |
30 |
Correct |
7 ms |
6048 KB |
Output is correct |
31 |
Correct |
6 ms |
5724 KB |
Output is correct |
32 |
Correct |
5 ms |
5588 KB |
Output is correct |
33 |
Correct |
5 ms |
5588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
5844 KB |
Output is correct |
7 |
Correct |
5 ms |
5836 KB |
Output is correct |
8 |
Correct |
5 ms |
5912 KB |
Output is correct |
9 |
Correct |
5 ms |
5716 KB |
Output is correct |
10 |
Correct |
4 ms |
5684 KB |
Output is correct |
11 |
Correct |
6 ms |
5788 KB |
Output is correct |
12 |
Correct |
5 ms |
5716 KB |
Output is correct |
13 |
Correct |
6 ms |
5844 KB |
Output is correct |
14 |
Correct |
6 ms |
5660 KB |
Output is correct |
15 |
Correct |
5 ms |
5972 KB |
Output is correct |
16 |
Correct |
5 ms |
5972 KB |
Output is correct |
17 |
Correct |
5 ms |
5716 KB |
Output is correct |
18 |
Correct |
7 ms |
6048 KB |
Output is correct |
19 |
Correct |
6 ms |
5724 KB |
Output is correct |
20 |
Correct |
5 ms |
5588 KB |
Output is correct |
21 |
Correct |
5 ms |
5588 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
6 ms |
6044 KB |
Output is correct |
25 |
Correct |
6 ms |
6100 KB |
Output is correct |
26 |
Correct |
5 ms |
6088 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
4 ms |
5828 KB |
Output is correct |
29 |
Correct |
4 ms |
5788 KB |
Output is correct |
30 |
Correct |
4 ms |
5660 KB |
Output is correct |
31 |
Correct |
4 ms |
5792 KB |
Output is correct |
32 |
Correct |
4 ms |
5664 KB |
Output is correct |
33 |
Correct |
96 ms |
46372 KB |
Output is correct |
34 |
Correct |
95 ms |
46328 KB |
Output is correct |
35 |
Correct |
4 ms |
6044 KB |
Output is correct |
36 |
Correct |
87 ms |
36808 KB |
Output is correct |
37 |
Correct |
93 ms |
36780 KB |
Output is correct |
38 |
Correct |
103 ms |
29552 KB |
Output is correct |
39 |
Correct |
103 ms |
38192 KB |
Output is correct |
40 |
Correct |
87 ms |
30528 KB |
Output is correct |
41 |
Correct |
116 ms |
29956 KB |
Output is correct |
42 |
Correct |
98 ms |
38240 KB |
Output is correct |
43 |
Correct |
145 ms |
30716 KB |
Output is correct |
44 |
Correct |
100 ms |
41336 KB |
Output is correct |
45 |
Correct |
101 ms |
34540 KB |
Output is correct |
46 |
Correct |
116 ms |
34016 KB |
Output is correct |
47 |
Correct |
110 ms |
33984 KB |
Output is correct |
48 |
Correct |
114 ms |
34028 KB |
Output is correct |
49 |
Correct |
99 ms |
33468 KB |
Output is correct |
50 |
Correct |
104 ms |
33736 KB |
Output is correct |
51 |
Correct |
266 ms |
48456 KB |
Output is correct |
52 |
Correct |
299 ms |
49324 KB |
Output is correct |
53 |
Correct |
211 ms |
47828 KB |
Output is correct |
54 |
Correct |
110 ms |
37528 KB |
Output is correct |
55 |
Correct |
109 ms |
37428 KB |
Output is correct |
56 |
Correct |
157 ms |
38740 KB |
Output is correct |
57 |
Correct |
143 ms |
32020 KB |
Output is correct |
58 |
Correct |
109 ms |
29520 KB |
Output is correct |
59 |
Correct |
185 ms |
35820 KB |
Output is correct |
60 |
Correct |
144 ms |
30748 KB |
Output is correct |
61 |
Correct |
279 ms |
40932 KB |
Output is correct |
62 |
Correct |
193 ms |
32068 KB |
Output is correct |
63 |
Correct |
167 ms |
42624 KB |
Output is correct |
64 |
Correct |
137 ms |
35508 KB |
Output is correct |
65 |
Correct |
152 ms |
34796 KB |
Output is correct |
66 |
Correct |
168 ms |
35204 KB |
Output is correct |
67 |
Correct |
171 ms |
35420 KB |
Output is correct |
68 |
Correct |
156 ms |
35036 KB |
Output is correct |
69 |
Correct |
223 ms |
35984 KB |
Output is correct |
70 |
Correct |
85 ms |
19656 KB |
Output is correct |
71 |
Correct |
151 ms |
36236 KB |
Output is correct |