dingyeye loves stone
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
BestCoder Round #90
思路:
本题本质为阶梯博弈。
思路:
本题本质为阶梯博弈。
阶梯博弈:博弈在一列阶梯上进行,每个阶梯上放着自然数个点,两个人进行阶梯博弈,每一步则是将一个集体上的若干个点
( >=1 )移到前面去,最后没有点可以移动的人输。
对于离root距离为偶数的点只要模仿对手的走法就行了。
对于离root距离为奇数的点把每个点的数量进行异或,得到的值不为0则先手赢,也就是Nim的过程。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 100005;
int n,m,i;
int u[maxn],v[maxn],w[maxn];
int first[maxn],nxt[maxn];
int ans =0;
void dfs(int st,int deep){
if(deep%2==1)
ans=ans^w[st];
//cout<<deep<<"deep"<<w[st]<<endl;
int k=first[st];
while(k!=-1) {
dfs(v[k],deep+1);
//printf("%d %d\n",u[k],v[k]);
k=nxt[k];
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(first,-1,sizeof(first));
int n;
int i,j;
scanf("%d",&n);
for(i=1;i<n;i++){
int a;
scanf("%d",&a);
u[i]=a;
v[i]=i;
nxt[i]=first[u[i]];
first[u[i]]=i;
}
for(i=0;i<n;i++){
int a;
scanf("%d",&a);
w[i]=a;
}
ans=0;
dfs(0,0);
if(ans!=0)
printf("win\n");
else
printf("lose\n");
}
return 0;
}
写完了之后看别人的代码发现,因为fa[i]<i ,所以只要子节点+=父节点的值,然后对O(n)扫一遍就行。
思维姜化啊!