CF2230F Game on Growing Tree 题解
:::::success[Hint 1]
考虑 Bob 的决策,以此设出 DP 状态。
:::::
:::::success[Hint 2]
直觉告诉我们答案不会太大,我们考虑证明答案上界。
:::::
对应 Hint 1:
我们考虑当 Alice 当前位于
对应 Hint 2:
直觉告诉我们最终答案不会很大,我们发现两个事实:
- 最终答案不会超过剩余可达的点数。
- 当 Alice 位于
u 点时,Bob 一种做法是直接堵上重儿子,这样 Bob 选一个儿子走下去可达点数至少减半。
因此答案和状态中的
这样我们就做完了这道题。
时间复杂度为
:::::success[code]
#include<bits/stdc++.h>
#define inl inline
#define int long long
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i)
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i)
#define endl '\n'
#define INF 1e16
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define lcm(x,y) x/__gcd(x,y)*y
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int>
#define gt(s) getline(cin,s)
#define at(x,y) for(const auto &x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
#ifdef __unix__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
using namespace std;
const int N=2e5+5,M=25;
int cnt[N][M],ans;
vector<int>g[N];
map<pii,bool>vis[M];
inl void dfs(int u,int v,int k){
ans=max(ans,k);
++cnt[v][k];
// cout<<u<<' '<<v<<' '<<k<<' '<<cnt[v][k]<<endl;
if(k==20) return;
if(cnt[v][k]==2){
ans=max(ans,k+1);
at(x,g[v])
if(!vis[k].count(prr(x,v))&&!vis[k+1].count(prr(v,x))){
vis[k+1][prr(v,x)]=1;
dfs(v,x,k+1);
}
}else if(cnt[v][k]==3){
at(x,g[v])
if(vis[k].count(prr(x,v))&&!vis[k+1].count(prr(v,x))){
vis[k+1][prr(v,x)]=1;
dfs(v,x,k+1);
}
}
}
signed main(){
fst;
int q;
cin>>q;
rep(i,2,q+1){
int ft;
cin>>ft;
g[ft].pb(i);
g[i].pb(ft);
vis[1][prr(i,ft)]=1;
vis[1][prr(ft,i)]=1;
rep(k,1,19){
if(cnt[ft][k]>=2&&!vis[k+1].count(prr(ft,i))){
vis[k+1][prr(ft,i)]=1;
dfs(ft,i,k+1);
}
}
dfs(i,ft,1);
dfs(ft,i,1);
cout<<ans<<' ';
}
return 0;
}
/*
11
1 2 1 2 2 2 7 6 9 4 9
*/
:::::