hdu 3094 A tree game 树上sg

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
2009 Multi-University Training Contest 18 - Host by ECNU

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

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

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include
#include
#include
#include<string>
#include
#include
#include
#include
#include
#include
#include<set>
#include
using namespace std;
#define LL __int64
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x)  cout<<"bug"<const int N=1e5+10,M=1e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;

vector<int>edge[N];
int sg[N];
void dfs(int u,int fa)
{
    for(int i=0;i)
    {
        int v=edge[u][i];
        if(v==fa)continue;
        dfs(v,u);
        sg[u]^=sg[v]+1;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(sg,0,sizeof(sg));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            edge[i].clear();
        for(int i=1;i)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            edge[u].push_back(v);
            edge[v].push_back(u);
        }
        dfs(1,-1);
        if(sg[1])printf("Alice\n");
        else printf("Bob\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/7391480.html

你可能感兴趣的:(java)