/*input
7 2
1
2
3
3
5
6
3 4
*/
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, m, has[maxn], dep[maxn], need[maxn], cnt[maxn], par[maxn];
vector<int>stk, ord;
vector<int>adj[maxn];
void prep(int v) {
stk.pb(v);
if (has[v]) {
++need[stk[dep[v] / 2 + 1]];
}
for (int to : adj[v]) {
prep(to);
}
stk.pop_back();
}
bool can(int min_dep) {
for (int v : ord) {
if (dep[v] >= min_dep) {
cnt[v]--;
}
if (cnt[v] > 0) {
return false;
}
cnt[par[v]] += cnt[v];
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
dep[i] = dep[p] + 1;
par[i] = p;
adj[p].pb(i);
}
for (int i = 1; i <= m; i++) {
int v;
cin >> v;
has[v] = 1;
}
for (int i = 1; i <= n; i++) {
ord.pb(i);
}
sort(all(ord), [&](int i, int j) {
return dep[i] > dep[j];
});
prep(1);
int lo = 0, hi = n - 1;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
for (int i = 1; i <= n; i++) {
cnt[i] = need[i];
}
if (can(mid)) {
lo = mid;
} else {
hi = mid - 1;
}
}
cout << lo + 1 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
9 ms |
5632 KB |
Output is correct |
4 |
Correct |
9 ms |
5632 KB |
Output is correct |
5 |
Correct |
9 ms |
5632 KB |
Output is correct |
6 |
Correct |
81 ms |
26092 KB |
Output is correct |
7 |
Correct |
79 ms |
26092 KB |
Output is correct |
8 |
Correct |
9 ms |
5632 KB |
Output is correct |
9 |
Correct |
99 ms |
28136 KB |
Output is correct |
10 |
Correct |
102 ms |
28396 KB |
Output is correct |
11 |
Correct |
92 ms |
27244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5376 KB |
Output is correct |
3 |
Correct |
8 ms |
5376 KB |
Output is correct |
4 |
Correct |
9 ms |
5300 KB |
Output is correct |
5 |
Correct |
11 ms |
5376 KB |
Output is correct |
6 |
Correct |
8 ms |
5248 KB |
Output is correct |
7 |
Correct |
81 ms |
26092 KB |
Output is correct |
8 |
Correct |
79 ms |
26092 KB |
Output is correct |
9 |
Correct |
9 ms |
5632 KB |
Output is correct |
10 |
Correct |
78 ms |
18072 KB |
Output is correct |
11 |
Correct |
76 ms |
18072 KB |
Output is correct |
12 |
Correct |
85 ms |
11620 KB |
Output is correct |
13 |
Correct |
83 ms |
19536 KB |
Output is correct |
14 |
Correct |
76 ms |
12652 KB |
Output is correct |
15 |
Correct |
79 ms |
11756 KB |
Output is correct |
16 |
Correct |
85 ms |
19564 KB |
Output is correct |
17 |
Correct |
126 ms |
12756 KB |
Output is correct |
18 |
Correct |
82 ms |
22252 KB |
Output is correct |
19 |
Correct |
81 ms |
16492 KB |
Output is correct |
20 |
Correct |
75 ms |
15984 KB |
Output is correct |
21 |
Correct |
77 ms |
15980 KB |
Output is correct |
22 |
Correct |
78 ms |
15980 KB |
Output is correct |
23 |
Correct |
78 ms |
15600 KB |
Output is correct |
24 |
Correct |
72 ms |
15860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5376 KB |
Output is correct |
7 |
Correct |
9 ms |
5376 KB |
Output is correct |
8 |
Correct |
9 ms |
5376 KB |
Output is correct |
9 |
Correct |
9 ms |
5248 KB |
Output is correct |
10 |
Correct |
9 ms |
5248 KB |
Output is correct |
11 |
Correct |
9 ms |
5504 KB |
Output is correct |
12 |
Correct |
9 ms |
5248 KB |
Output is correct |
13 |
Correct |
9 ms |
5376 KB |
Output is correct |
14 |
Correct |
9 ms |
5376 KB |
Output is correct |
15 |
Correct |
9 ms |
5504 KB |
Output is correct |
16 |
Correct |
9 ms |
5504 KB |
Output is correct |
17 |
Correct |
9 ms |
5376 KB |
Output is correct |
18 |
Correct |
9 ms |
5504 KB |
Output is correct |
19 |
Correct |
9 ms |
5376 KB |
Output is correct |
20 |
Correct |
8 ms |
5376 KB |
Output is correct |
21 |
Correct |
8 ms |
5376 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
7 ms |
5120 KB |
Output is correct |
24 |
Correct |
9 ms |
5632 KB |
Output is correct |
25 |
Correct |
9 ms |
5632 KB |
Output is correct |
26 |
Correct |
9 ms |
5632 KB |
Output is correct |
27 |
Correct |
8 ms |
5120 KB |
Output is correct |
28 |
Correct |
8 ms |
5376 KB |
Output is correct |
29 |
Correct |
8 ms |
5376 KB |
Output is correct |
30 |
Correct |
9 ms |
5300 KB |
Output is correct |
31 |
Correct |
11 ms |
5376 KB |
Output is correct |
32 |
Correct |
8 ms |
5248 KB |
Output is correct |
33 |
Correct |
9 ms |
5632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5376 KB |
Output is correct |
7 |
Correct |
9 ms |
5376 KB |
Output is correct |
8 |
Correct |
9 ms |
5376 KB |
Output is correct |
9 |
Correct |
9 ms |
5248 KB |
Output is correct |
10 |
Correct |
9 ms |
5248 KB |
Output is correct |
11 |
Correct |
9 ms |
5504 KB |
Output is correct |
12 |
Correct |
9 ms |
5248 KB |
Output is correct |
13 |
Correct |
9 ms |
5376 KB |
Output is correct |
14 |
Correct |
9 ms |
5376 KB |
Output is correct |
15 |
Correct |
9 ms |
5504 KB |
Output is correct |
16 |
Correct |
9 ms |
5504 KB |
Output is correct |
17 |
Correct |
9 ms |
5376 KB |
Output is correct |
18 |
Correct |
9 ms |
5504 KB |
Output is correct |
19 |
Correct |
9 ms |
5376 KB |
Output is correct |
20 |
Correct |
8 ms |
5376 KB |
Output is correct |
21 |
Correct |
8 ms |
5376 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
7 ms |
5120 KB |
Output is correct |
24 |
Correct |
9 ms |
5632 KB |
Output is correct |
25 |
Correct |
9 ms |
5632 KB |
Output is correct |
26 |
Correct |
9 ms |
5632 KB |
Output is correct |
27 |
Correct |
8 ms |
5120 KB |
Output is correct |
28 |
Correct |
8 ms |
5376 KB |
Output is correct |
29 |
Correct |
8 ms |
5376 KB |
Output is correct |
30 |
Correct |
9 ms |
5300 KB |
Output is correct |
31 |
Correct |
11 ms |
5376 KB |
Output is correct |
32 |
Correct |
8 ms |
5248 KB |
Output is correct |
33 |
Correct |
81 ms |
26092 KB |
Output is correct |
34 |
Correct |
79 ms |
26092 KB |
Output is correct |
35 |
Correct |
9 ms |
5632 KB |
Output is correct |
36 |
Correct |
78 ms |
18072 KB |
Output is correct |
37 |
Correct |
76 ms |
18072 KB |
Output is correct |
38 |
Correct |
85 ms |
11620 KB |
Output is correct |
39 |
Correct |
83 ms |
19536 KB |
Output is correct |
40 |
Correct |
76 ms |
12652 KB |
Output is correct |
41 |
Correct |
79 ms |
11756 KB |
Output is correct |
42 |
Correct |
85 ms |
19564 KB |
Output is correct |
43 |
Correct |
126 ms |
12756 KB |
Output is correct |
44 |
Correct |
82 ms |
22252 KB |
Output is correct |
45 |
Correct |
81 ms |
16492 KB |
Output is correct |
46 |
Correct |
75 ms |
15984 KB |
Output is correct |
47 |
Correct |
77 ms |
15980 KB |
Output is correct |
48 |
Correct |
78 ms |
15980 KB |
Output is correct |
49 |
Correct |
78 ms |
15600 KB |
Output is correct |
50 |
Correct |
72 ms |
15860 KB |
Output is correct |
51 |
Correct |
99 ms |
28136 KB |
Output is correct |
52 |
Correct |
102 ms |
28396 KB |
Output is correct |
53 |
Correct |
92 ms |
27244 KB |
Output is correct |
54 |
Correct |
81 ms |
18712 KB |
Output is correct |
55 |
Correct |
81 ms |
18712 KB |
Output is correct |
56 |
Correct |
85 ms |
19692 KB |
Output is correct |
57 |
Correct |
86 ms |
13548 KB |
Output is correct |
58 |
Correct |
71 ms |
11624 KB |
Output is correct |
59 |
Correct |
95 ms |
17268 KB |
Output is correct |
60 |
Correct |
79 ms |
13676 KB |
Output is correct |
61 |
Correct |
110 ms |
21868 KB |
Output is correct |
62 |
Correct |
118 ms |
14440 KB |
Output is correct |
63 |
Correct |
90 ms |
23660 KB |
Output is correct |
64 |
Correct |
84 ms |
17772 KB |
Output is correct |
65 |
Correct |
85 ms |
17264 KB |
Output is correct |
66 |
Correct |
85 ms |
17644 KB |
Output is correct |
67 |
Correct |
92 ms |
17644 KB |
Output is correct |
68 |
Correct |
94 ms |
16624 KB |
Output is correct |
69 |
Correct |
91 ms |
17776 KB |
Output is correct |
70 |
Correct |
55 ms |
10096 KB |
Output is correct |
71 |
Correct |
83 ms |
19184 KB |
Output is correct |