HDU 3094 A tree game 树删边游戏

本文介绍了一种使用递归深度优先搜索方法计算博弈树中各节点的SG值的算法实现。该算法适用于无信息状态的游戏树,通过XOR运算求得非叶节点的SG值,最终判断初始节点的SG值来决定游戏胜负。

叶节点SG值至0 非叶节点SG值至于它的所有子节点SG值添加1 XOR和后

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector <int> G[100010];
int sg[100010];
int dfs(int x, int f)
{
	if(sg[x] != -1)
		return sg[x];
	if(!G[x].size())
		return 0;
	int ans = 0;
	for(int i = 0; i < G[x].size(); i++)
	{
		int v = G[x][i];
		if(v == f)
			continue;
	ans ^= dfs(v, x)+1;
	}
	return ans;
}
int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		memset(sg, -1, sizeof(sg));
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
			G[i].clear();
		for(int i = 1; i < n; i++)
		{
			int u, v;
			scanf("%d %d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		int ans = dfs(1, -1);
		if(ans)
			puts("Alice");
		else
			puts("Bob");
	}
	return 0;
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值