Gameia
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1181 Accepted Submission(s): 506
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?
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
T≤100
1≤N≤500
0≤K≤500
1≤Pi≤i
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
T≤100
1≤N≤500
0≤K≤500
1≤Pi≤i
Output
For each test case output one line denotes the answer.
If Alice can win, output "Alice" , otherwise "Bob".
If Alice can win, output "Alice" , otherwise "Bob".
Sample Input
2 2 1 1 3 1 1 2
Sample Output
Bob
Alice
Alice
以下为官方题解,已经很详细了就不在解释了
如果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只能在游戏开始之前做小动作”会得到同样的结论。
- “氪不改命,玄不救非”
总结来说,若存在两两匹配的方案,Alice就从父节点往下染就一定赢,不存在的话,若Bob不能把所有两两匹配的点对分开,那么Alice从某个叶子结点(该叶子结点是它父节点唯一的孩子)开始染就一定赢。
这样只要在树中找能否所有点能找到两两匹配的方案就可以了,用DFS
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN =510;
int n;
vector<int> map[MAXN];
int dfs(int x)
{
if(map[x].empty()) //叶子节点返回一个未匹配节点
{
return 1;
}
int tmp,unmatch=0;
for(int i=0;i<map[x].size();i++)
{
tmp=dfs(map[x][i]);
if(tmp==-1)
return -1;
unmatch+=tmp;
}
if(unmatch==1) //若该节点子节点还有一个没匹配,则与该节点匹配,返回0
{
return 0;
}
else if(unmatch==0) //若该节点子节点全部都完成匹配,在返回一个未匹配点(它自己)
{
return 1;
}
else //若该节点子节点有很多未匹配节点,则返回匹配失败
{
return -1;
}
}
int main()
{
int t,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) map[i].clear();
int x;
for(int i=2;i<=n;i++)
{
scanf("%d",&x);
map[x].push_back(i);
}
int tmp=dfs(1);
if(tmp==0)
{
if(n/2<=k+1)
printf("Bob\n");
else
printf("Alice\n");
}
else
printf("Alice\n");
}
return 0;