dingyeye loves stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 121 Accepted Submission(s): 72
Problem Description
dingyeye loves play stone game with you.
dingyeye has an n -point tree.The nodes are numbered from0 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
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 integern 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 that0≤fa[i]<i .
The next line contains n integersa[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 thatn>100 .
In each test case,the first line contains one integer
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
The next line contains n integers
1≤T≤100 , 1≤n≤100000 .
It is guaranteed that there is at most 7 test cases such that
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
题目大意:
dingyeye喜欢和你玩石子游戏。dingyeye有一棵
n
个节点的有根树,节点编号为
解题思路:
设根节点的深度为
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;
const int MAXN = 1e5+5;
typedef long long LL;
LL a[MAXN];
vector<LL>vec[MAXN];
int n;
void Init(){
for(int i=0; i<=n; i++)
vec[i].clear();
}
LL ok = 0;
void dfs(int u, int dep)
{
if(dep & 1)
ok ^= a[u];
for(int i=0; i<vec[u].size(); i++)
{
LL k = vec[u][i];
dfs(k,dep+1);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
Init();
for(int i=1; i<n; i++){
LL x;
scanf("%I64d",&x);
vec[x].push_back(i);
}
for(int i=0; i<n; i++)
scanf("%I64d",&a[i]);
ok = 0;
dfs(0, 0);
if(ok)
puts("win");
else
puts("lose");
}
return 0;
}