hdu 5996 dingyeye loves stone

来源:互联网 发布:广发证券炒股软件 编辑:程序博客网 时间:2024/05/16 05:17

dingyeye loves stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 330 Accepted Submission(s): 186

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.

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]

//hdu 5996#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=100005;int n,T,cnt,ans;int head[mxn],a[mxn],fa[mxn];struct edge {int to,next;} f[mxn];inline void add(int u,int v){    f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;}inline void dfs(int u,int dep){    if(dep&1) ans^=a[u];    for(int i=head[u];i;i=f[i].next)    {        int v=f[i].to;        dfs(v,dep+1);    }}int main(){    int i,j;    scanf("%d",&T);    while(T--)    {        ans=cnt=0;        M(head);        scanf("%d",&n);        fo(i,1,n-1)        {            scanf("%d",&fa[i]);            add(fa[i],i);        }        fo(i,0,n-1) scanf("%d",&a[i]);        dfs(0,0);        if(ans==0) printf("lose\n");        else printf("win\n");    }    return 0;}