Gameia(HDU 6105)

Gameia

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1055    Accepted Submission(s): 451


Problem Description
Alice and Bob are playing a game called 'Gameia ? Gameia !'. The game goes like this :
0. There is a tree with all node unpainted initial.
1. Because Bob is the VIP player, so Bob has K chances to make a small change on the tree any time during the game if he wants, whether before or after Alice's action. These chances can be used together or separate, changes will happen in a flash. each change is defined as cut an edge on the tree. 
2. Then the game starts, Alice and Bob take turns to paint an unpainted node, Alice go first, and then Bob.
3. In Alice's move, she can paint an unpainted node into white color.
4. In Bob's move, he can paint an unpainted node into black color, and what's more, all the other nodes which connects with the node directly will be painted or repainted into black color too, even if they are white color before.
5. When anybody can't make a move, the game stop, with all nodes painted of course. If they can find a node with white color, Alice win the game, otherwise Bob.
Given the tree initial, who will win the game if both players play optimally?
 

Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with two integers N and K : the size of the tree and the max small changes that Bob can make.
The next line gives the information of the tree, nodes are marked from 1 to N, node 1 is the root, so the line contains N-1 numbers, the i-th of them give the farther node of the node i+1.

Limits
T100
1N500
0K500
1Pii
 

Output
For each test case output one line denotes the answer.
If Alice can win, output "Alice" , otherwise "Bob".
 

Sample Input
  
  
2 2 1 1 3 1 1 2
 

Sample Output
  
  
Bob Alice


 //题意:Bob和Alice玩游戏,有一棵树,两人给树染色,Bob黑色,Alice白色,两人只能染树上未染色的点。Bob是氪金玩家,他能断树上的k条边(<=k均可),并且他把树上的一个节点染成黑色后,与这个节点相连的所有点都会变成黑色(包括Alice染的白色点)。Alice只能给一个点染成白色。若树上的节点已全部染色完毕,如果有白色点Alice胜,否则Bob胜。

//思路(官方题解)

  • 如果Bob能把这棵树分成若干两个一组的点对,那么Bob取得胜利,否则Alice获胜。
  • 如果原树不存在两两匹配的方案,Alice从树叶开始,每次都染树叶父节点,Bob被迫只能不断的染叶子,Bob退化成一般玩家,因为Bob做不做小动作都不会逆转局势,总会出现一个时间点Bob没办法跟上Alice的节奏而让Alice染到一个周围都已被染色的孤立点(因为原树不存在两两匹配的方案)
  • 如果原树存在两两匹配的方案,而且Bob的小动作次数也足以把原树分成两两的点对,那么Bob显然获胜。
  • 如果原树存在两两匹配的方案,而Bob的小动作不足以把树分成两两的点对,Alice一定获胜,因为每次染某个叶子节点(该节点为其父节点的唯一子节点),Alice总能迫使Bob不断的做小动作以保证剩下的树不会出现奇数节点的树,且每次小动作割出一个点对(包含Alice刚染的点),最后有两种情况。
  • 出现某个结点有>=2个子节点为叶子节点。Alice染这个点,Bob跟不上Alice的节奏,出现孤点,Ailice取胜
  • 否则整个过程一定会持续到树被染光或者Bob被Alice掏空导致做不了小动作进而被迫割出一块size为奇数的子树(这棵树显然没办法两两匹配)而败北。
  • Bob被允许“任意时刻”做小动作看似很厉害其实很鸡肋,把问题改成“Bob只能在游戏开始之前做小动作”会得到同样的结论。
  • “氪不改命,玄不救非”
氪不改命,玄不救非!

两两配对指的是一个点能找到一个与它相连的点,且每个点只能用一次。

怎么判断一棵树能不能两两配对?一个dfs即可,时间复杂度O(n)。从叶子节点开始判断,叶子节点必然只能与其父亲节点配对,已配对的点标记一下,会产生新的“叶子节点”(它的节点都已经配对,它只能和它的父亲节点去配对),如果有点无法配对,那这棵树就不能两两配对。

若这棵树能两两配对,那么要把它分成两个点一组的若干棵树,要断n/2 - 1条边。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

const int MAX = 500 + 10;

int n, k;
vector<int>map[MAX];
bool flag;
int vis[MAX];

//root表示当前结点,fa表示当前节点的父亲节点
void dfs(int root, int fa)
{
	//leaf
	if (map[root].size() == 0)
	{
		//如果叶子节点的父亲节点没有与其他的点配对
		//那叶子节点就和它的父亲节点配对
		if (vis[fa] == 0)
		{
			vis[root] = 1;
			vis[fa] = 1;
		}
		//不然的话这张图就无法两两配对
		else
		{
			flag = 0;
		}
		return;
	}
	//else
	for (int i = 0; i < map[root].size(); i++)
	{
		dfs(map[root][i], root);
	}
	//如果一个节点的子节点能两两配对
	//那么这个节点就要和它的父亲节点配对
	if (vis[root] == 0)
	{
		if (vis[fa] == 0)
		{
			vis[root] = 1;
			vis[fa] = 1;
		}
		else
		{
			flag = 0;
		}
	}
}

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &n, &k);
		for (int i = 0; i <= n; i++)
			map[i].clear();
		for (int i = 2; i <= n; i++)
		{
			int x;
			scanf("%d", &x);
			map[x].push_back(i);
		}
		memset(vis, 0, sizeof(vis));
		//假设1的父亲是0,并且0不能用来配对
		vis[0] = 1;
		flag = 1;
		//dfs用来判断这棵树是否可以两两配对
		dfs(1, 0);
		if (flag == 1)
		{
			//如果能把这棵树切成两两配对,Bob赢,不然Alice赢
			if (k >= (n / 2 - 1))
				printf("Bob\n");
			else
				printf("Alice\n");
		}
		else
			printf("Alice\n");
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值