hdu3094 A tree game

67 篇文章 1 订阅

A tree game

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 424 Accepted Submission(s): 207


Problem Description
Alice and Bob want to play an interesting game on a tree.
Given is a tree on N vertices, The vertices are numbered from 1 to N. vertex 1 represents the root. There are N-1 edges. Players alternate in making moves, Alice moves first. A move consists of two steps. In the first step the player selects an edge and removes it from the tree. In the second step he/she removes all the edges that are no longer connected to the root. The player who has no edge to remove loses.
You may assume that both Alice and Bob play optimally.

Input
The first line of the input file contains an integer T (T<=100) specifying the number of test cases.
Each test case begins with a line containing an integer N (1<=N<=10^5), the number of vertices,The following N-1 lines each contain two integers I , J, which means I is connected with J. You can assume that there are no loops in the tree.

Output
For each case, output a single line containing the name of the player who will win the game.

Sample Input
  
  
3 3 1 2 2 3 3 1 2 1 3 10 6 2 4 3 8 4 9 5 8 6 2 7 5 8 1 9 6 10

Sample Output
  
  
Alice Bob Alice
组合游戏略述 ——浅谈SG游戏的若干拓展及变形 论文上说的很详细了!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define M 100500
vector<int > vec[M];
int vis[M],dp[M];
int dfs(int x){
    if(dp[x]!=-1)return dp[x];
    vis[x]=1;
    dp[x]=0;
    for(int i=0;i<vec[x].size();i++){
        if(!vis[vec[x][i]])
        dp[x]^=dfs(vec[x][i])+1;
    }
    return dp[x];
}
int main()
{
    int tcase,n,i,s,e;
    scanf("%d",&tcase);
    while(tcase--){
        scanf("%d",&n);
        for(i=0;i<=n;i++)vec[i].clear();
        memset(vis,0,sizeof(vis));
        memset(dp,-1,sizeof(dp));
        n--;
        while(n--){
            scanf("%d%d",&s,&e);
            vec[s].push_back(e);
            vec[e].push_back(s);
        }
        if(dfs(1))printf("Alice\n");
        else printf("Bob\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值