#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define f first
#define s second
#define pb push_back
#define sz(x) ((int)((x).size()))
#define le(vec) vec[vec.size()-1]
#define all(x) (x).begin(), (x).end()
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef long double ld;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
const int N=2e5+10, L=19;
int n, m, par[N][L], dp[N], dol[N];
vi ad[N], dollas;
void pre(int u, int pst)
{
for(auto x : ad[u]){
if(x==pst) continue;
dp[x]=dp[u]+1;
par[x][0]=u;
pre(x, u);
}
}
void build()
{
FOR(i, 1, L)
{
FOR(j, 0, n)
{
par[j][i]=par[par[j][i-1]][i-1];
}
}
}
int lift(int u, int d)
{
for(int i=L-1; i>=0; i--){
if(d&(1<<i))
u=par[u][i];
}
return u;
}
void place()
{
for(auto x : dollas){
dol[lift(x, (dp[x]-1)/2)]++;
}
}
bool ok;
int tr(int u, int pst, int x)
{
int tot=0;
for(auto v : ad[u]){
if(v==pst) continue;
tot+=tr(v, u, x);
}
if(dp[u]>=x) tot++;
if(tot<dol[u]) ok=false;
tot-=dol[u];
return tot;
}
bool can(int x)
{
ok=true;
tr(0,-1,x);
return ok;
}
int main()
{
setIO();
cin >> n >> m;
FOR(i, 0, n-1)
{
int x; cin >> x;
ad[x-1].pb(i+1);
ad[i+1].pb(x-1);
}
FOR(i, 0, m)
{
int x; cin >> x;
dollas.pb(x-1);
}
pre(0,-1);
build();
place();
int l=0, r=n;
while(l<r){
int m=(l+r+1)/2;
if(can(m)) l=m;
else r=m-1;
}
cout << l+1;
}
Compilation message
mokesciai.cpp: In function 'void setIO(std::string)':
mokesciai.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mokesciai.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4936 KB |
Output is correct |
3 |
Correct |
6 ms |
5968 KB |
Output is correct |
4 |
Correct |
5 ms |
6000 KB |
Output is correct |
5 |
Correct |
5 ms |
6004 KB |
Output is correct |
6 |
Correct |
150 ms |
43776 KB |
Output is correct |
7 |
Correct |
153 ms |
43716 KB |
Output is correct |
8 |
Correct |
4 ms |
5920 KB |
Output is correct |
9 |
Correct |
226 ms |
45808 KB |
Output is correct |
10 |
Correct |
241 ms |
46380 KB |
Output is correct |
11 |
Correct |
187 ms |
45056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
43776 KB |
Output is correct |
2 |
Correct |
153 ms |
43716 KB |
Output is correct |
3 |
Correct |
4 ms |
5920 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
4 ms |
5708 KB |
Output is correct |
7 |
Correct |
5 ms |
5540 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5580 KB |
Output is correct |
10 |
Correct |
118 ms |
34748 KB |
Output is correct |
11 |
Correct |
110 ms |
34620 KB |
Output is correct |
12 |
Correct |
247 ms |
28052 KB |
Output is correct |
13 |
Correct |
147 ms |
36072 KB |
Output is correct |
14 |
Correct |
98 ms |
27988 KB |
Output is correct |
15 |
Correct |
269 ms |
28356 KB |
Output is correct |
16 |
Correct |
147 ms |
35976 KB |
Output is correct |
17 |
Correct |
362 ms |
28192 KB |
Output is correct |
18 |
Correct |
139 ms |
37668 KB |
Output is correct |
19 |
Correct |
141 ms |
29168 KB |
Output is correct |
20 |
Correct |
152 ms |
28316 KB |
Output is correct |
21 |
Correct |
159 ms |
28356 KB |
Output is correct |
22 |
Correct |
152 ms |
28356 KB |
Output is correct |
23 |
Correct |
210 ms |
28228 KB |
Output is correct |
24 |
Correct |
154 ms |
28196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4936 KB |
Output is correct |
3 |
Correct |
6 ms |
5968 KB |
Output is correct |
4 |
Correct |
5 ms |
6000 KB |
Output is correct |
5 |
Correct |
5 ms |
6004 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
4 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
5 ms |
5540 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
4 ms |
5580 KB |
Output is correct |
12 |
Correct |
4 ms |
5920 KB |
Output is correct |
13 |
Correct |
3 ms |
5020 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
2 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
5708 KB |
Output is correct |
19 |
Correct |
4 ms |
5708 KB |
Output is correct |
20 |
Correct |
5 ms |
5836 KB |
Output is correct |
21 |
Correct |
5 ms |
5580 KB |
Output is correct |
22 |
Correct |
5 ms |
5580 KB |
Output is correct |
23 |
Correct |
5 ms |
5708 KB |
Output is correct |
24 |
Correct |
5 ms |
5580 KB |
Output is correct |
25 |
Correct |
5 ms |
5708 KB |
Output is correct |
26 |
Correct |
6 ms |
5532 KB |
Output is correct |
27 |
Correct |
4 ms |
5808 KB |
Output is correct |
28 |
Correct |
5 ms |
5836 KB |
Output is correct |
29 |
Correct |
4 ms |
5524 KB |
Output is correct |
30 |
Correct |
5 ms |
5752 KB |
Output is correct |
31 |
Correct |
4 ms |
5580 KB |
Output is correct |
32 |
Correct |
5 ms |
5544 KB |
Output is correct |
33 |
Correct |
4 ms |
5484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
5708 KB |
Output is correct |
7 |
Correct |
4 ms |
5708 KB |
Output is correct |
8 |
Correct |
5 ms |
5836 KB |
Output is correct |
9 |
Correct |
5 ms |
5580 KB |
Output is correct |
10 |
Correct |
5 ms |
5580 KB |
Output is correct |
11 |
Correct |
5 ms |
5708 KB |
Output is correct |
12 |
Correct |
5 ms |
5580 KB |
Output is correct |
13 |
Correct |
5 ms |
5708 KB |
Output is correct |
14 |
Correct |
6 ms |
5532 KB |
Output is correct |
15 |
Correct |
4 ms |
5808 KB |
Output is correct |
16 |
Correct |
5 ms |
5836 KB |
Output is correct |
17 |
Correct |
4 ms |
5524 KB |
Output is correct |
18 |
Correct |
5 ms |
5752 KB |
Output is correct |
19 |
Correct |
4 ms |
5580 KB |
Output is correct |
20 |
Correct |
5 ms |
5544 KB |
Output is correct |
21 |
Correct |
4 ms |
5484 KB |
Output is correct |
22 |
Correct |
2 ms |
4940 KB |
Output is correct |
23 |
Correct |
2 ms |
4936 KB |
Output is correct |
24 |
Correct |
6 ms |
5968 KB |
Output is correct |
25 |
Correct |
5 ms |
6000 KB |
Output is correct |
26 |
Correct |
5 ms |
6004 KB |
Output is correct |
27 |
Correct |
3 ms |
5012 KB |
Output is correct |
28 |
Correct |
4 ms |
5708 KB |
Output is correct |
29 |
Correct |
4 ms |
5708 KB |
Output is correct |
30 |
Correct |
5 ms |
5540 KB |
Output is correct |
31 |
Correct |
4 ms |
5708 KB |
Output is correct |
32 |
Correct |
4 ms |
5580 KB |
Output is correct |
33 |
Correct |
150 ms |
43776 KB |
Output is correct |
34 |
Correct |
153 ms |
43716 KB |
Output is correct |
35 |
Correct |
4 ms |
5920 KB |
Output is correct |
36 |
Correct |
118 ms |
34748 KB |
Output is correct |
37 |
Correct |
110 ms |
34620 KB |
Output is correct |
38 |
Correct |
247 ms |
28052 KB |
Output is correct |
39 |
Correct |
147 ms |
36072 KB |
Output is correct |
40 |
Correct |
98 ms |
27988 KB |
Output is correct |
41 |
Correct |
269 ms |
28356 KB |
Output is correct |
42 |
Correct |
147 ms |
35976 KB |
Output is correct |
43 |
Correct |
362 ms |
28192 KB |
Output is correct |
44 |
Correct |
139 ms |
37668 KB |
Output is correct |
45 |
Correct |
141 ms |
29168 KB |
Output is correct |
46 |
Correct |
152 ms |
28316 KB |
Output is correct |
47 |
Correct |
159 ms |
28356 KB |
Output is correct |
48 |
Correct |
152 ms |
28356 KB |
Output is correct |
49 |
Correct |
210 ms |
28228 KB |
Output is correct |
50 |
Correct |
154 ms |
28196 KB |
Output is correct |
51 |
Correct |
226 ms |
45808 KB |
Output is correct |
52 |
Correct |
241 ms |
46380 KB |
Output is correct |
53 |
Correct |
187 ms |
45056 KB |
Output is correct |
54 |
Correct |
115 ms |
35372 KB |
Output is correct |
55 |
Correct |
129 ms |
35348 KB |
Output is correct |
56 |
Correct |
139 ms |
37524 KB |
Output is correct |
57 |
Correct |
115 ms |
29116 KB |
Output is correct |
58 |
Correct |
243 ms |
28156 KB |
Output is correct |
59 |
Correct |
171 ms |
32828 KB |
Output is correct |
60 |
Correct |
309 ms |
29916 KB |
Output is correct |
61 |
Correct |
249 ms |
38280 KB |
Output is correct |
62 |
Correct |
395 ms |
29724 KB |
Output is correct |
63 |
Correct |
169 ms |
38760 KB |
Output is correct |
64 |
Correct |
157 ms |
30056 KB |
Output is correct |
65 |
Correct |
170 ms |
29164 KB |
Output is correct |
66 |
Correct |
186 ms |
29628 KB |
Output is correct |
67 |
Correct |
182 ms |
29560 KB |
Output is correct |
68 |
Correct |
242 ms |
29312 KB |
Output is correct |
69 |
Correct |
220 ms |
30068 KB |
Output is correct |
70 |
Correct |
60 ms |
18068 KB |
Output is correct |
71 |
Correct |
141 ms |
33512 KB |
Output is correct |