HDU 6105 (2017 多校训练赛6 1010)Gameia(博弈)

2017 Multi-University Training Contest - Team 6 1010


Gameia

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


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
 

题意:

Alice和Bob玩游戏,有一个树,A可以选一个未染色结点染成白色  B可以在任何时候切断K条连边,B可以选一个未染色结点染成黑色
同时这个结点连接的点也会变成黑色。
最后还有白色的结点那么A赢否则B赢。
分析:
一开始觉得B很容易赢,但分析后发现,B其实只有在这颗树只有2个结点的时候才能赢否则必定输(不能切割的时候)
因为刚开始的时候每个人的轮次是一样的,所以每次A走完B必定要覆盖A的点,那么只要有一回合A有空隙,或者A能分割B的决策
那么A必赢。
接着每切一刀相当将一颗树分成两颗,所以B要赢只有每颗数都是两个节点。
那么直接从叶子结点找父亲结点切割,看需要多少刀就可以得到答案啦~

AC代码:
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
using namespace std;
const int maxn=2000+5;
int father[maxn];
int vis[maxn];
vector<int>g[maxn];

int du[maxn],n;

int toposort()
{
    memset(du,0,sizeof(du));
    memset(vis,0,sizeof(vis));
    int ans=0;
    for(int i=1;i<=n;i++)
    {
    for(int j=0;j<g[i].size();j++)
    {
        du[i]++;
    }
    }
    queue<int>Q;
    for(int i=1;i<=n;i++)
    {
    if(!du[i])
    {
        Q.push(i);
    }
    }
    while(!Q.empty())
    {
    int x=Q.front();
    Q.pop();
    int t=father[father[x]];
//    printf("x=%d t=%d\n",x,t);
    if(father[x]!=-1&&!vis[x]&&!vis[father[x]])
    {
        du[t]--;
        vis[x]=1;
        vis[father[x]]=1;
        ans++;
    }
    else
    {
        return -1;
    }
    if(!du[t])
    Q.push(t);
    
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
    for(int i=0;i<maxn;i++)
        g[i].clear();
    memset(father,-1,sizeof(father));
    int k;
    scanf("%d%d",&n,&k);
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&father[i]);
        g[father[i]].push_back(i);
    }
    int ans=toposort();
//    printf("!  %d\n",ans-1);
    if(ans==-1||ans-1>k)
        printf("Alice\n");
    else
        printf("Bob\n");
    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值