HDU 2017 多校联合训练赛6 1010 6105 Gameia

来源:互联网 发布:淘宝购物可以开发票吗 编辑:程序博客网 时间:2024/06/09 18:14

Gameia

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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
22 113 11 2
 

Sample Output
BobAlice
 

Source
2017 Multi-University Training Contest - Team 6


题目大意
给你一棵大小为n的未被涂色的树,根节点为1,现在,Alice和Bob要给这棵树依次涂色,Alice先手,每次给一个没有被涂色的节点涂白色,然后是Bob给没有被涂色的节点涂黑色,并且与Bob涂黑的节点直接相邻的节点也会被变成黑色,由于Bob是VIP,所以他还有k次特权,能在任意时刻,砍断树上的任意一条边,当树上没有可以被涂色的节点的时候,游戏结束。如果树上没有白色节点,Bob赢,反之,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只能在游戏开始之前做小动作”会得到同样的结论。
  • “氪不改命,玄不救非”

时间复杂度 O(n)O(n)



题目分析
首先,对详细的官方题解表示感谢。
自己写几组简单的数据,比如,3,4,5,6个节点的树,模拟一下博弈,我们会发现,无论是哪一种情况,只要Bob不使用砍树的特权,都是Alice获胜。

如果要保证Bob赢,就必须这棵树有偶数个节点,并且能把这棵树砍成若干两个一组的点对。

而且,Bob每次涂色都要保证涂在Alice刚刚涂色的白色节点旁边,才能保证所有的白色节点能被覆盖。而Alice可以利用这一特点,从树叶开始,每次都染树叶父节点,Bob被迫只能不断的染叶子,Bob退化成一般玩家,因为Bob做不做小动作都不会逆转局势。最终,这棵树未被涂色的部分会变成简单的奇数个节点的树。

还有一点,如果出现某个结点有>=2个子节点为叶子节点。Alice染这个点,、Bob只能涂其中一个叶节点,那么至少一个叶节点成为孤点,不与其他未被涂色的点直接相连,那么Alice必胜,即使总结点数为偶数,并且2*(k+1)>=n。
在判断一个父节点的叶节点数时,要注意,题目默认节点1为根节点,所以在遍历叶节点时,1节点是不会被处理的。但是,如果出现根节点只连接了一个子节点的情况,根节点实际是可以被看做叶节点的,所以根节点要特殊处理。


代码
#include <cstdio>#include <cstring>using namespace std;int T, n, k;int f[505];//f[i]:记录节点i的父节点int sz[505];//sz[i]:以i为父节点的子节点数(不一定为叶节点)int szl[505];//szl[i]:以i为父节点的叶节点数int son1;int main(){    //freopen("1010in.txt","r",stdin);    //freopen("1010out1.txt","w",stdout);    scanf ("%d",&T);    while (T--)    {        memset(f, 0, sizeof(f));        memset(sz, 0, sizeof(sz));        memset(szl, 0, sizeof(szl));        scanf ("%d %d",&n,&k);        for (int i=2; i<=n; i++)        {            scanf ("%d",&f[i]);            sz[f[i]] ++;            if (f[i] == 1)                son1 = i;//记录根节点的子节点,如果子节点只有一个,根节点可以当作叶子节点看待,如果子节点有多个,则此变量无用        }        int flag = 1;        if (sz[1] == 1)            szl[son1] ++;        for (int i=2; i<=n; i++)        {            if (sz[i] == 0)            {                szl[f[i]] ++;                if (szl[f[i]] >= 2)                {                    flag = 0;                    break;                }            }        }        if (flag)        {            if (n%2 == 0 && 2*(k+1) >= n)                flag = 1;            else                flag = 0;        }        if (flag)            printf ("Bob\n");        else            printf ("Alice\n");    }    return 0;}




原创粉丝点击