【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;
}
【hdu 5996】dingyeye loves stone的更多相关文章
- HDU 5996:dingyeye loves stone(阶梯博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=5996 题意:在一棵树上进行博弈,每次只能将当前的结点的石子放到父节点上,最后不能移动的输. 思路:比赛的时候想的 ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- 【hdu 2486】A simple stone game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 【HDOJ5996】dingyeye loves stone(Nim游戏)
题意:dingyeye喜欢和你玩石子游戏.dingyeye有一棵n个节点的有根树,节点编号为0到n−1,根为0号节点. 游戏开始时,第i个节点上有a[i]个石子.两位玩家轮流操作,每次操作玩家可以选择 ...
- 【HDU 6021】 MG loves string (枚举+容斥原理)
MG loves string Accepts: 30 Submissions: 67 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- 【HDU 6020】 MG loves apple (乱搞?)
MG loves apple Accepts: 20 Submissions: 693 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: ...
- 【2014 Multi-University Training Contest 2 1002】/【HDU 4873】 ZCC Loves Intersection
果然,或滥用零件,啥都不说了.我们欣慰地学习阅读.这两天残疾儿童是数学. 这是求所需的问题,不明确.贴上官方的解题报告. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...
- hdu 5996 dingyeye loves stone(博弈)
题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你 ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
随机推荐
- C#调用C++数组,结构体DLL
1.基本数据类型的传递 常见数据类型的传递 C/C++ C# 长度 short short 2Bytes int int 4Bytes long(该类型在传递的时候常常会弄混) int 4Bytes ...
- 【2017"百度之星"程序设计大赛 - 初赛(A)】今夕何夕
[链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=775&pid=1005 [题意] 在这里写题意 [题 ...
- zico源代码分析(二) 数据读取和解析部分
第一部分:分析篇 首先,看一下zico的页面,左侧是hostname panel,右侧是该主机对应的traces panel. 点击左侧zorka主机名,右侧panel会更新信息,在火狐浏览器中使用f ...
- selenium 自动化基础知识(各种定位)
元素的定位 webdriver 提供了一很多对象定位方法 例如: [ id ] , name , class name , link text , partial link text , tag n ...
- MongoDb 查询时常用方法
Query.All("name", "a", "b");//通过多个元素来匹配数组Query.And(Query.EQ("name ...
- 【例题5-4 UVA - 156】Ananagrams
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个字符串如果每个字符按照升序排一下.假设他们能够互相变化. 则肯定是一样的. 根据这个东西,用一个map来判重就好. [错的次数] ...
- 推荐一下《聊聊JVM》的专栏
依照惯例新开了一个专栏后要单推一下.推荐一下<聊聊JVM的专栏>,网上关于JVM的文章太多,这个专栏希望能在已有的资料的基础上写出点新意,对一些重要的概念归纳总结,说说自己的观点.理解和实 ...
- UIBarButtonItem使用
1 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBar ...
- 通过WPF中UserControl内的按钮点击关闭父窗体
原文:通过WPF中UserControl内的按钮点击关闭父窗体 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article ...
- WindowImplBase::OnSysCommand-------duilib在最大化和还原间切换
virtual LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if( wPar ...