hdu 3094 A tree game (树形删边游戏)

来源:互联网 发布:网络派出所电话 编辑:程序博客网 时间:2024/06/06 03:24

A tree game

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


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
331 22 331 21 3 106 24 38 49 58 62 75 81 96 10
 

Sample Output
AliceBobAlice
 

Source
2009 Multi-University Training Contest 18 - Host by ECNU
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1907 2873 2509 1760 2147 


题解:树形删边游戏

两点性质:1.所有叶节点的sg值为0

2.其他节点的sg值为他子节点的sg值+1的异或和。

证明来自贾志豪《组合游戏略述——浅谈SG游戏的若干拓展及变形》

 


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 200003using namespace std;int tot,n,m,point[N],nxt[N],v[N];void add(int x,int y){tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;}int dfs(int x,int fa){int sg=0;for (int i=point[x];i;i=nxt[i]) if (v[i]!=fa)   sg^=(dfs(v[i],x)+1);return sg;}int main(){scanf("%d",&m);for (int t=1;t<=m;t++) {scanf("%d",&n);tot=0;memset(point,0,sizeof(point));for (int i=1;i<n;i++){int x,y; scanf("%d%d",&x,&y);add(x,y);}int ans=dfs(1,0);if (ans) printf("Alice\n");else printf("Bob\n");}}



0 0
原创粉丝点击