This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |