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;
    }
    
  • 相关阅读:
    【面试题】Round A China New Grad Test 2014总结
    【C++】指针数组和数组指针
    快速排序算法递归和非递归实现
    StringTokenizer的用法
    java实时监测文件夹的变化,允许多用户同时访问,完成文件转移
    java统计当前在线数
    KMP算法的一种实现
    java.io.PrintWriter
    OOAOODOOP
    Java 编程技术中汉字问题的分析及解决
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626713.html
Copyright © 2011-2022 走看看