zoukankan      html  css  js  c++  java
  • 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

    贾志豪《组合游戏略述——浅谈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+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<edge[u].size();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<n;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
    ");
            else printf("Bob
    ");
        }
        return 0;
    }
  • 相关阅读:
    Spring Data Rest如何暴露ID字段
    Windows空间清理2
    把爱好变成职业
    面对面的口头信息传递对人决策的影响力最大
    最好是更好的敌人
    文明主线
    钱的本质
    2019第42周日
    开源与商业化
    生涯四度
  • 原文地址:https://www.cnblogs.com/jhz033/p/7391480.html
Copyright © 2011-2022 走看看