跳到正文

题解

题解:AT_abc427_d [ABC427D] The Simple Game

Small_Label_First约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年12月18日
收录
2025年12月18日
更新
2026年11月13日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

既然他们都会按照最优策略移动,那么有一下策略:

  • 当 Alice 走棋时,只要有一个结果能使 Alice 赢,那么 Alice 就会按照能使她赢的策略来移动。
  • 当 Bob 走棋时,只要有一个结果能使 Bob 赢,那么 Bob 就会按照能使他赢的策略来移动。

注意到 k≤10k\le10,因此至多只会产出深度为 21 的 dfs,那么我们就可以写分层图来AC此题啦。

初始时若 Alice 移动,则默认 Bob 赢,否则默认 Alice 赢。

依次往上转移即可。

不要抄袭
C++
#include <bits/stdc++.h>
using namespace std;
int t, n, m, k, a[200005][25];
vector<int> g[200005];
char s[200005];
void dfs(int u, int cg){
	if(cg==2 * k + 1){
		a[u][cg] = s[u] - 'A' + 1;
		return;
	}
	if(cg % 2){
		a[u][cg] = 2;
	}else{
		a[u][cg] = 1;
	}
	for(auto v : g[u]){
		if(!a[v][cg + 1]){
			dfs(v, cg + 1);
		}
		if(cg % 2){
			if(a[v][cg + 1]==1){
				a[u][cg] = 1;
			}
		}else{
			if(a[v][cg + 1]==2){
				a[u][cg] = 2;
			}
		}
	}
}
void solve(){
	scanf("%d %d %d %s", &n, &m, &k, s + 1);
	for(int i = 1;i<=m;i++){
		int x, y;
		scanf("%d %d", &x, &y);
		g[x].push_back(y);
	}
	dfs(1, 1);
	printf("%s\n", a[1][1]==1 ? "Alice" : "Bob");
	for(int i = 1;i<=n;i++){
		g[i].clear();
		s[i] = 0;
		for(int j = 1;j<=2 * k + 1;j++){
			a[i][j] = 0;
		}
	}
}
int main(){
	scanf("%d", &t);
	while(t--){
		solve();
	}
	return 0;
}
原文