zoukankan      html  css  js  c++  java
  • 【hdu 5996】dingyeye loves stone

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 208 Accepted Submission(s): 119

    Problem Description
    dingyeye loves play stone game with you.

    dingyeye has an n-point tree.The nodes are numbered from 0 to n−1,while the root is numbered 0.Initially,there are a[i] stones on the i-th node.The game is in turns.When one move,he can choose a node and move some(this number cannot be 0) of the stones on it to its father.One loses the game if he can’t do anything when he moves.

    You always move first.You want to know whether you can win the game if you play optimally.

    Input
    In the first line, there is an integer T indicating the number of test cases.

    In each test case,the first line contains one integer n refers to the number of nodes.

    The next line contains n−1 integers fa[1]⋯fa[n−1],which describe the father of nodes 1⋯n−1(node 0 is the root).It is guaranteed that 0≤fa[i] < i.

    The next line contains n integers a[0]⋯a[n−1],which describe the initial stones on each nodes.It is guaranteed that 0≤a[i]< 134217728.

    1≤T≤100,1≤n≤100000.

    It is guaranteed that there is at most 7 test cases such that n>100.

    Output
    For each test case output one line.If you can win the game,print “win”.Ohterwise,print “lose”.

    Sample Input
    2
    2
    0
    1000 1
    4
    0 1 0
    2 3 3 3

    Sample Output
    win
    lose

    【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=5996

    【题解】

    可以转化为阶梯博弈;
    对深度为偶数的节点进行操作;
    相当于一个NIM博弈;
    把石子扔掉;
    (如果对方对深度为奇数的节点操作,那么你总能把对方扔到偶数深度节点上的石头等量地移动到深度为奇数的节点上);
    始终保持偶数深度上节点的石头为你的必胜态(你)或必败态(对方);

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int MAXN = 100000+100;
    
    LL a[MAXN],dd;
    int n,totm;
    int fir[MAXN],nex[MAXN*2],en[MAXN*2];
    
    void dfs(int x,int ndep)
    {
        if (ndep%2==0)
            dd^=a[x];
        for (int temp = fir[x];temp;temp = nex[temp])
        {
            int y = en[temp];
            dfs(y,ndep+1);
        }
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        int T;
        rei(T);
        while (T--)
        {
            totm = 0;
            rei(n);
            rep1(i,0,n-1)
                fir[i] = 0;
            rep1(i,1,n-1)
            {
                int fa;
                rei(fa);
                nex[++totm] = fir[fa];
                fir[fa] = totm;
                en[totm] = i;
            }
            rep1(i,0,n-1)
                rel(a[i]);
            dd = 0;
            dfs(0,1);
            if (dd!=0)
                puts("win");
            else
                puts("lose");
        }
        return 0;
    }
    
  • 相关阅读:
    撬动百亿VRAR产业,让VR们“造”起来
    带你熟悉鸿蒙轻内核Kconfig使用指南
    教你Python字符串的基本操作:拆分和连接
    从翻硬币游戏看敏捷开发
    求助,请教各位,如何牵头做一个项目
    Qt三方库开发技术:QXlsx介绍、编译和使用
    项目实战:Qt+ffmpeg摄像头检测工具
    OpenSSL 自述
    用故事说透HTTPS
    一起来看看大佬是怎样配置nginx虚拟主机
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626713.html
Copyright © 2011-2022 走看看