hdu 3094 A tree game 树上sg

时间:2024-04-04 13:07:51

A tree game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
Source

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

树上基础博弈:sg[u]={sg[v]+1}异或和 u是v的父亲;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL __int64
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; vector<int>edge[N];
int sg[N];
void dfs(int u,int fa)
{
for(int i=;i<edge[u].size();i++)
{
int v=edge[u][i];
if(v==fa)continue;
dfs(v,u);
sg[u]^=sg[v]+;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(sg,,sizeof(sg));
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
edge[i].clear();
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(,-);
if(sg[])printf("Alice\n");
else printf("Bob\n");
}
return ;
}