跳到正文

题解

CF2230F Game on Growing Tree 题解

zrl123456约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年5月25日
更新
2026年7月10日
篇幅
约 6 分钟
获赞
3
题目
CF2230F

版本

  1. v1当前
Hint 1

考虑 Bob 的决策,以此设出 DP 状态。

Hint 2

直觉告诉我们答案不会太大,我们考虑证明答案上界。

对应 Hint 1:

我们考虑当 Alice 当前位于 uu 点,容易发现 Bob 一定选择 uu 的一个相邻的点 vv 进行染色来完全防止 Alice 进入一个子树,那么我们可以设 fu,v,kf_{u,v,k} 表示目前位于 uu 但是不能往 vv 这个邻点走并且现在 uu 是走到的第 kk 个点是否可行,转移方程是简单的,fu,v,kf_{u,v,k} 合法当且仅当存在两个 fx,u,k−1f_{x,u,k-1}(v≠xv\ne x)合法,这个暴力转移是简单的,细节可以见代码。

对应 Hint 2:

直觉告诉我们最终答案不会很大,我们发现两个事实:

  • 最终答案不会超过剩余可达的点数。
  • 当 Alice 位于 uu 点时,Bob 一种做法是直接堵上重儿子,这样 Bob 选一个儿子走下去可达点数至少减半。

因此答案和状态中的 kk 最多是 log⁡n\log n 的,上面的做法就能过了。

这样我们就做完了这道题。
时间复杂度为 Θ(qlog⁡2q)\Theta(q\log^2q),空间复杂度为 Θ(qlog⁡q)\Theta(q\log q)。

code
C++
#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

*/ 
原文
存档评论6 条抓取于