dingyeye loves stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 54 Accepted Submission(s): 30
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
某大牛思路:
如这就是一个阶梯博弈的初始状态 2 1 3 2 4 ... 只能把后面的点往前面放...如何来分析这个问题呢...其实阶梯博弈经过转换可以变为Nim..
把所有奇数阶梯看成N堆石子..做nim..把石子从奇数堆移动到偶数堆可以理解为拿走石子..就相当于几个奇数堆的石子在做Nim..( 如所给样例..2^3^4=5 不为零所以先手必败)为什么可以这样来转化?
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define maxn 100005
#define INF 2e15
typedef long long ll;
using namespace std;
vector<int> v[maxn];
int num[maxn], ans = 0;
void dfs(int j, int h){
for(int i = 0; i < v[j].size(); i++){
int k = v[j][i];
if((h+1)&1)
ans ^= num[k];
dfs(k, h+1);
}
}
int main(){
// freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--){
ans = 0;
int n, a;
scanf("%d", &n);
for(int i = 1; i < n; i++){
scanf("%d", &a);
v[a].push_back(i);
}
for(int i = 0; i < n; i++)
scanf("%d", num+i);
dfs(0, 0);
if(ans)
puts("win");
else
puts("lose");
for(int i = 0; i < n; i++)
v[i].clear();
}
return 0;
}