您的位置:首页 > 其它

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

2016-12-23 11:03 393 查看

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


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each case, output a single line containing the name of the player who will win the game.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

Alice
Bob
Alice

 

[align=left]Source[/align]
2009 Multi-University Training Contest 18 - Host by ECNU

 

[align=left]Recommend[/align]
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 200003
using namespace std;
int tot,n,m,point
,nxt
,v
;
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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: