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

【hdu 5996】dingyeye loves stone的更多相关文章

  1. HDU 5996:dingyeye loves stone(阶梯博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5996 题意:在一棵树上进行博弈,每次只能将当前的结点的石子放到父节点上,最后不能移动的输. 思路:比赛的时候想的 ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. 【hdu 2486】A simple stone game

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  4. 【HDOJ5996】dingyeye loves stone(Nim游戏)

    题意:dingyeye喜欢和你玩石子游戏.dingyeye有一棵n个节点的有根树,节点编号为0到n−1,根为0号节点. 游戏开始时,第i个节点上有a[i]个石子.两位玩家轮流操作,每次操作玩家可以选择 ...

  5. 【HDU 6021】 MG loves string (枚举+容斥原理)

    MG loves string  Accepts: 30  Submissions: 67  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  6. 【HDU 6020】 MG loves apple (乱搞?)

    MG loves apple  Accepts: 20  Submissions: 693  Time Limit: 3000/1500 MS (Java/Others)  Memory Limit: ...

  7. 【2014 Multi-University Training Contest 2 1002】/【HDU 4873】 ZCC Loves Intersection

    果然,或滥用零件,啥都不说了.我们欣慰地学习阅读.这两天残疾儿童是数学. 这是求所需的问题,不明确.贴上官方的解题报告. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...

  8. hdu 5996 dingyeye loves stone(博弈)

    题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你 ...

  9. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

随机推荐

  1. hdu5301(2015多校2)--Buildings(构造)

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  2. 41.关于Intellij IDEA菜单项中Compile、Make和Build的区别

    转自:https://www.cnblogs.com/ini_always/archive/2011/10/23/2221985.html Compile.Make和Build的区别   针对Java ...

  3. 1.3 Quick Start中 Step 2: Start the server官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 2: Start the server Step : 启动服务 Kafka ...

  4. 洛谷 P2069 松鼠吃果子

    P2069 松鼠吃果子 题目描述 有N个一种松鼠喜欢吃的果子由下向上串排成一列,并标号1,2,...N.一只松鼠从最下果子开始向上跳,并且第i次跳可以一次跳过i*i*i除以5的余数+1个果子(=i*i ...

  5. Tomcat源代码阅读#1:classloader初始化

    Bootstrap 通过Tomcat的启动脚本能够看到启动的入口是在Bootstrap,来看下Bootstrap的main方法, /** * Main method and entry point w ...

  6. 多线程在python中的使用 thread

    近期想学习研究一下python中使用多线程,来提高python在爬虫项目中的效率. 如今我们在网页上查询到在python中使用的多线程的使用大多数都是使用的threading模块,可是python中另 ...

  7. 哈夫曼树的介绍 ---java实现

    一.     什么是哈夫曼树 是一种带权路径长度最短的二叉树,也称最优二叉树 带权路径长度:WPL=(W1*L1+W2*L2+W3*L3+...+ Wn*Ln) N个权值Wi(i=1,2,...n)构 ...

  8. Zookeeper源码用ant进行编译为eclipse工程--转载

    原文地址:http://www.it165.net/os/html/201411/10142.html Zookeeper GitHub的下载地址是:https://github.com/apache ...

  9. jQuery的原理

    JQ的原理 jquery-1.xxx :专门为PC端诞生的类库,兼容所有的浏览器 jquery-2.xxx:当初是为了移动端而准备的,所以IE低版本浏览器一般不兼容,但是这个版本针对移动端的事件等操作 ...

  10. 洛谷—— P1168 中位数

    https://www.luogu.org/problem/show?pid=1168 题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], ...