hdu 6105 Gameia(树形DP)


Gameia

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


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


题解:http://bestcoder.hdu.edu.cn/blog/

a[u]=0,表示这个点已经合法,a[u]=1,表示这个点的子节点合法 当前点还在分割 ,a[u]=2表示当前点不合法;

主要就是节点的状态需要考虑清楚

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include <bits/stdc++.h>
using namespace std;
const int N =100000+10;
vector<int>p[N];
int a[N];
void dfs(int u,int fa)
{
    int cnt=0;
    if(p[u].size()==0)
    {
        a[u]=1;
        return ;
    }
    for(int i=0;i<p[u].size();i++)
    {
        int v=p[u][i];
        dfs(v,u);
        if(a[v]==1) cnt++;
        if(a[v]==2)  {a[u]=2; return ;}
    }
    if(cnt>1) a[u]=2;
    else if(cnt==1) a[u]=0;
    else if(cnt==0) a[u]=1;
    return ;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, k;
        scanf("%d %d", &n, &k);
        for(int i=0;i<=n;i++) p[i].clear();
        for(int i=2;i<=n;i++)
        {
            int x;
            scanf("%d", &x);
            p[x].push_back(i);
        }
        a[1]=2;
        dfs(1,-1);
        if(a[1]==0&&(n-2)/2<=k) puts("Bob");
        else puts("Alice");
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值