#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], par[MAXN][maxLog], 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) / 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;
pos = true;
possible(1, 0, r - 1);
if (pos) 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;
dfs(1, 0);
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mokesciai.cpp: In function 'int solve()':
mokesciai.cpp:74:1: warning: control reaches end of non-void function [-Wreturn-type]
74 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
5 ms |
5972 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |