2017 Multi-University Training Contest 6 && HDOJ 6105 Gameia 【树形博弈】

来源:互联网 发布:微信现场抽奖软件 编辑:程序博客网 时间:2024/06/05 12:44

Gameia

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

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 afterAlice's action. These chances can be used together or separate, changes willhappen 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, otherwiseBob.
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 treeand 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 thefarther node of the node i+1.

Limits
T≤100
1≤
N≤500
0≤
K≤500
1≤
Pii

 

 

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

 

【题意】


有一棵一开始没有颜色的树,Alice和Bob轮流对其染色(Alice先手),Alice每次可以把一个没有颜色的节点染成白色,Bob每次可以把一个没有颜色的节点染成黑色,并且与其直接连接的点无论有颜色与否都会变成黑色,且Bob还可以有一个技能,他能在任何时刻切断两个点之间的连接,共可以切k次。有一个人无法操作,游戏结束,这时树上如果有白色的节点,Alice获胜,否则Bob获胜。


【思路】

官方题解:




我们通过总结发现只要满足下面三个任意一个条件Alice就必胜,否则Bob获胜

  1. Bob的技能使用次数不足以把所有节点分成若干两个一组的点对。
  2. 节点个数为奇数。
  3. 存在大于等于两个节点的父亲节点是同一个。
  4. 某个节点存在大于等于两个子节点,其下面节点的个数(包括自身)为奇数。(感谢GDUT_JMC )


原因分析

  1. 只有在两个一组的情况下,Alice把一个点染成白色,Bob把另一个点染成黑色,同时前一个点也变成了黑色,以此类推,Bob能获胜,否则Bob无法每次都把Alice染的白色变为黑色。
  2. 奇数的话,其余都成了两个一组,并能全部为黑色,但剩下的一个点会被Alice染成白色,而Bob无法操作了。
  3. 这种情况下,Alice只要把父亲节点染为白色,然后Bob只能染其中一个叶子节点,虽然父亲节点也变成了黑色,但只要Alice把另一个叶子节点染成白色,Bob对这个点就无可奈何了(注意前提是叶子节点)。
  4. 这样的话,由于子节点下的节点个数为奇数,这样的点只有一个时,我可以加上它的父节点使这些点的个数为偶数(暂不考虑子树中还有不满足的情况),但大于等于两个的话,必然留下至少一棵子树节点个数为奇数(条件2),即Alice必胜。


    #include <cstdio>#include <vector>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn = 505;const ll mod = 1e9+7;const int INF = 0x3f3f3f;const double eps = 1e-9;int n,k;int flag;vector<int>vec[maxn];int dfs(int u){    int num=0;    int cnt=1;                 //该点及以下构成的树节点个数    int Count=0;               //统计子节点构成子树的节点个数为奇数的个数    for(int i=0;i<vec[u].size();i++)    {        int v=vec[u][i];        if(vec[v].size()==0)   //判断是否为叶子结点            num++;        if(num>=2)             //两个及以上叶子结点的父亲节点是同一个,Alice必胜        {            flag=1;            return 0;        }        int temp=dfs(v);        cnt+=temp;        if(temp&1)            Count++;        if(Count>=2)        {            flag=1;            return 0;        }    }    if(flag) return 0;    return cnt;}int main(){    int x;    rush()    {        scanf("%d%d",&n,&k);        for(int i=0;i<=n;i++)        {            vec[i].clear();        }        for(int i=2;i<=n;i++)        {            scanf("%d",&x);            vec[x].push_back(i);        }        flag=0;        dfs(1);        if(n&1||n/2-1>k||flag==1)        {            puts("Alice");        }        else puts("Bob");    }    return 0;}