dingyeye loves stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 137 Accepted Submission(s): 82
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.
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 .
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
建一棵树,每个节点有石子,每次可以移动石子到父节点,最后没得移动的那个人输,问先手能不能赢
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
/*
B 求每个点的深度.
设根节点的深度为0,将所有深度为奇数的节点的石子数目xor起来,
则先手必胜当且仅当这个xor和不为0。
证明同阶梯博弈。
对于偶深度的点上的石子,若对手移动它们,则可模仿操作;
对于奇深度上的石子,移动一次即进入偶深度的点。
时空复杂度O(n)。
*/
const int maxn = 1e5 + 5;
int fa[maxn];
int deep[maxn];
int stone[maxn];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
memset(fa, 0, sizeof(fa));
memset(deep, 0, sizeof(deep));
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++)
{
scanf("%d", &fa[i]);
deep[i] = deep[fa[i]]+1;
}
int ans = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &stone[i]);
if (deep[i] & 1) ans ^= stone[i];
}
if (ans != 0)
puts("win");
else puts("lose");
}
return 0;
}