#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6+6, inf = LLONG_MAX;
int n, m, sz, ans, dol[N], dol2[N], up[N], dep[N];
vector<int> path, adj[N];
bitset<N> vis;
set<pair<int, int>> st[N];
void dfs3(int x, int idx, int d) {
if (vis[x]) return;
vis[x] = 1;
auto it = st[idx].find(make_pair(-dep[x], x));
if (it != st[idx].end()) st[idx].erase(it);
for (int& y : adj[x]) {
if (y == up[x]) continue;
dfs3(y, idx, d+1);
}
}
void dfs2(int x) {
st[x].insert(make_pair(-dep[x], x));
for (int& y : adj[x]) {
if (y == up[x]) continue;
dfs2(y);
if (st[y].size() > st[x].size()) swap(st[x], st[y]);
for (pair<int, int> p : st[y]) {
st[x].insert(p);
}
st[y].clear();
}
//assert(dol2[x] <= st[x].size());
while (dol2[x] > 0) {
//cerr << x << " " << dol2[x] << endl;
dol2[x]--;
auto it = st[x].begin();
int d = it->first;
int y = it->second;
st[x].erase(it);
ans = min(ans, -d);
st[x].insert(make_pair(d+1, up[y]));
dfs3(y, x, 0);
}
}
void dfs(int x, int u, int d) {
up[x] = u;
dep[x] = d;
path.push_back(x);
if (dol[x] && x) {
dol[x]--;
int sz = path.size();
dol2[path[sz - sz/2]]++;
}
for (int& y : adj[x]) {
if (y == u) continue;
dfs(y, x, d+1);
}
path.pop_back();
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
sz = 0;
ans = inf;
memset(dol, 0, sizeof(dol));
memset(dol2, 0, sizeof(dol2));
cin >> n >> m;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
x--;
adj[x].push_back(i);
adj[i].push_back(x);
}
bool ok = 1;
for (int i = 0; i < m; i++) {
int x;
cin >> x;
x--;
dol[x]++;
if (!x) ok = 0;
}
if (n == 1 || !ok) {
cout << "1\n";
return 0;
}
dfs(0, 0, 1);
dfs2(0);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
91988 KB |
Output is correct |
2 |
Correct |
18 ms |
91996 KB |
Output is correct |
3 |
Correct |
20 ms |
93344 KB |
Output is correct |
4 |
Correct |
21 ms |
93276 KB |
Output is correct |
5 |
Correct |
20 ms |
93276 KB |
Output is correct |
6 |
Correct |
114 ms |
145572 KB |
Output is correct |
7 |
Correct |
128 ms |
145768 KB |
Output is correct |
8 |
Correct |
20 ms |
93256 KB |
Output is correct |
9 |
Correct |
137 ms |
145656 KB |
Output is correct |
10 |
Correct |
146 ms |
145600 KB |
Output is correct |
11 |
Correct |
130 ms |
145852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
145572 KB |
Output is correct |
2 |
Correct |
128 ms |
145768 KB |
Output is correct |
3 |
Correct |
20 ms |
93256 KB |
Output is correct |
4 |
Correct |
17 ms |
92164 KB |
Output is correct |
5 |
Correct |
21 ms |
93016 KB |
Output is correct |
6 |
Correct |
19 ms |
93020 KB |
Output is correct |
7 |
Correct |
20 ms |
92504 KB |
Output is correct |
8 |
Correct |
19 ms |
92656 KB |
Output is correct |
9 |
Correct |
23 ms |
92508 KB |
Output is correct |
10 |
Correct |
110 ms |
131524 KB |
Output is correct |
11 |
Correct |
110 ms |
131520 KB |
Output is correct |
12 |
Correct |
188 ms |
114008 KB |
Output is correct |
13 |
Correct |
125 ms |
129984 KB |
Output is correct |
14 |
Correct |
192 ms |
118788 KB |
Output is correct |
15 |
Correct |
181 ms |
114144 KB |
Output is correct |
16 |
Correct |
100 ms |
130100 KB |
Output is correct |
17 |
Correct |
307 ms |
115236 KB |
Output is correct |
18 |
Correct |
126 ms |
137440 KB |
Output is correct |
19 |
Correct |
124 ms |
115524 KB |
Output is correct |
20 |
Correct |
113 ms |
113416 KB |
Output is correct |
21 |
Correct |
109 ms |
113160 KB |
Output is correct |
22 |
Correct |
109 ms |
113408 KB |
Output is correct |
23 |
Correct |
110 ms |
113092 KB |
Output is correct |
24 |
Correct |
117 ms |
112724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
91988 KB |
Output is correct |
2 |
Correct |
18 ms |
91996 KB |
Output is correct |
3 |
Correct |
20 ms |
93344 KB |
Output is correct |
4 |
Correct |
21 ms |
93276 KB |
Output is correct |
5 |
Correct |
20 ms |
93276 KB |
Output is correct |
6 |
Correct |
17 ms |
92164 KB |
Output is correct |
7 |
Correct |
21 ms |
93016 KB |
Output is correct |
8 |
Correct |
19 ms |
93020 KB |
Output is correct |
9 |
Correct |
20 ms |
92504 KB |
Output is correct |
10 |
Correct |
19 ms |
92656 KB |
Output is correct |
11 |
Correct |
23 ms |
92508 KB |
Output is correct |
12 |
Correct |
20 ms |
93256 KB |
Output is correct |
13 |
Correct |
19 ms |
91996 KB |
Output is correct |
14 |
Correct |
17 ms |
92072 KB |
Output is correct |
15 |
Correct |
18 ms |
91840 KB |
Output is correct |
16 |
Correct |
20 ms |
91992 KB |
Output is correct |
17 |
Correct |
18 ms |
91996 KB |
Output is correct |
18 |
Correct |
23 ms |
93024 KB |
Output is correct |
19 |
Correct |
19 ms |
92760 KB |
Output is correct |
20 |
Correct |
20 ms |
92764 KB |
Output is correct |
21 |
Correct |
25 ms |
92364 KB |
Output is correct |
22 |
Correct |
23 ms |
92508 KB |
Output is correct |
23 |
Correct |
20 ms |
92860 KB |
Output is correct |
24 |
Correct |
20 ms |
92252 KB |
Output is correct |
25 |
Correct |
23 ms |
92764 KB |
Output is correct |
26 |
Correct |
24 ms |
92508 KB |
Output is correct |
27 |
Correct |
22 ms |
92888 KB |
Output is correct |
28 |
Correct |
20 ms |
92992 KB |
Output is correct |
29 |
Correct |
20 ms |
92508 KB |
Output is correct |
30 |
Correct |
22 ms |
92760 KB |
Output is correct |
31 |
Correct |
20 ms |
92356 KB |
Output is correct |
32 |
Correct |
20 ms |
92752 KB |
Output is correct |
33 |
Correct |
19 ms |
92508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
91996 KB |
Output is correct |
2 |
Correct |
17 ms |
92072 KB |
Output is correct |
3 |
Correct |
18 ms |
91840 KB |
Output is correct |
4 |
Correct |
20 ms |
91992 KB |
Output is correct |
5 |
Correct |
18 ms |
91996 KB |
Output is correct |
6 |
Correct |
23 ms |
93024 KB |
Output is correct |
7 |
Correct |
19 ms |
92760 KB |
Output is correct |
8 |
Correct |
20 ms |
92764 KB |
Output is correct |
9 |
Correct |
25 ms |
92364 KB |
Output is correct |
10 |
Correct |
23 ms |
92508 KB |
Output is correct |
11 |
Correct |
20 ms |
92860 KB |
Output is correct |
12 |
Correct |
20 ms |
92252 KB |
Output is correct |
13 |
Correct |
23 ms |
92764 KB |
Output is correct |
14 |
Correct |
24 ms |
92508 KB |
Output is correct |
15 |
Correct |
22 ms |
92888 KB |
Output is correct |
16 |
Correct |
20 ms |
92992 KB |
Output is correct |
17 |
Correct |
20 ms |
92508 KB |
Output is correct |
18 |
Correct |
22 ms |
92760 KB |
Output is correct |
19 |
Correct |
20 ms |
92356 KB |
Output is correct |
20 |
Correct |
20 ms |
92752 KB |
Output is correct |
21 |
Correct |
19 ms |
92508 KB |
Output is correct |
22 |
Correct |
33 ms |
91988 KB |
Output is correct |
23 |
Correct |
18 ms |
91996 KB |
Output is correct |
24 |
Correct |
20 ms |
93344 KB |
Output is correct |
25 |
Correct |
21 ms |
93276 KB |
Output is correct |
26 |
Correct |
20 ms |
93276 KB |
Output is correct |
27 |
Correct |
17 ms |
92164 KB |
Output is correct |
28 |
Correct |
21 ms |
93016 KB |
Output is correct |
29 |
Correct |
19 ms |
93020 KB |
Output is correct |
30 |
Correct |
20 ms |
92504 KB |
Output is correct |
31 |
Correct |
19 ms |
92656 KB |
Output is correct |
32 |
Correct |
23 ms |
92508 KB |
Output is correct |
33 |
Correct |
114 ms |
145572 KB |
Output is correct |
34 |
Correct |
128 ms |
145768 KB |
Output is correct |
35 |
Correct |
20 ms |
93256 KB |
Output is correct |
36 |
Correct |
110 ms |
131524 KB |
Output is correct |
37 |
Correct |
110 ms |
131520 KB |
Output is correct |
38 |
Correct |
188 ms |
114008 KB |
Output is correct |
39 |
Correct |
125 ms |
129984 KB |
Output is correct |
40 |
Correct |
192 ms |
118788 KB |
Output is correct |
41 |
Correct |
181 ms |
114144 KB |
Output is correct |
42 |
Correct |
100 ms |
130100 KB |
Output is correct |
43 |
Correct |
307 ms |
115236 KB |
Output is correct |
44 |
Correct |
126 ms |
137440 KB |
Output is correct |
45 |
Correct |
124 ms |
115524 KB |
Output is correct |
46 |
Correct |
113 ms |
113416 KB |
Output is correct |
47 |
Correct |
109 ms |
113160 KB |
Output is correct |
48 |
Correct |
109 ms |
113408 KB |
Output is correct |
49 |
Correct |
110 ms |
113092 KB |
Output is correct |
50 |
Correct |
117 ms |
112724 KB |
Output is correct |
51 |
Correct |
137 ms |
145656 KB |
Output is correct |
52 |
Correct |
146 ms |
145600 KB |
Output is correct |
53 |
Correct |
130 ms |
145852 KB |
Output is correct |
54 |
Correct |
137 ms |
133056 KB |
Output is correct |
55 |
Correct |
110 ms |
128112 KB |
Output is correct |
56 |
Correct |
116 ms |
131820 KB |
Output is correct |
57 |
Correct |
157 ms |
111952 KB |
Output is correct |
58 |
Correct |
166 ms |
115372 KB |
Output is correct |
59 |
Correct |
134 ms |
119912 KB |
Output is correct |
60 |
Correct |
178 ms |
112420 KB |
Output is correct |
61 |
Correct |
134 ms |
131192 KB |
Output is correct |
62 |
Correct |
221 ms |
113492 KB |
Output is correct |
63 |
Correct |
120 ms |
134344 KB |
Output is correct |
64 |
Correct |
123 ms |
114808 KB |
Output is correct |
65 |
Correct |
116 ms |
113436 KB |
Output is correct |
66 |
Correct |
130 ms |
109900 KB |
Output is correct |
67 |
Correct |
118 ms |
112328 KB |
Output is correct |
68 |
Correct |
127 ms |
114096 KB |
Output is correct |
69 |
Correct |
106 ms |
107088 KB |
Output is correct |
70 |
Correct |
109 ms |
106068 KB |
Output is correct |
71 |
Correct |
139 ms |
121800 KB |
Output is correct |