【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;
}

转载于:https://www.cnblogs.com/AWCXV/p/7626713.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值