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
Source
详细搜阶梯博弈
就是把所有奇数层的石头数异或起来,看是否为0
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct line
{
int s,t;
int next;
}a[200001];
int head[100001];
int edge;
inline void add(int s,int t)
{
a[edge].next=head[s];
head[s]=edge;
a[edge].s=s;
a[edge].t=t;
}
int dep[100001],s[100001];
inline void trdp(int d)
{
int i;
for(i=head[d];i!=0;i=a[i].next)
{
int t=a[i].t;
dep[t]=dep[d]+1;
trdp(t);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T>0)
{
T--;
int n;
scanf("%d",&n);
edge=0;
memset(head,0,sizeof(head));
int i;
int t;
for(i=2;i<=n;i++)
{
scanf("%d",&t);
edge++;
add(t+1,i);
}
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
dep[1]=0;
trdp(1);
int s1=0,s2=0;
for(i=2;i<=n;i++)
{
/* if(s[i]>1)
{
if(dep[i]==1)
s1++;
else
s2++;
}
else if(s[i]==1)
{
if(dep[i]%2==1)
s1++;
else
s2++;
}*/
if(dep[i]%2==1)
{
s1=(s1^s[i]);
}
}
// printf("lose\n");
// printf("win\n");
if(s1==0)
{
//if(s2%2==0)
printf("lose\n");
// else
// printf("win\n");
}
else
{
// if(s2%2==0)
// else
printf("win\n");
}
}
return 0;
}