HDU 5996 dingyeye loves stone (DFS+博弈)

本文介绍了一种通过深度优先搜索和异或运算判断石子游戏胜负的方法。游戏中玩家轮流从树形结构的节点中移动石子到其父节点,直至无法移动为止。文章提供了完整的C++代码实现。

dingyeye loves stone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 98    Accepted Submission(s): 56


Problem Description
dingyeye loves play stone game with you.

dingyeye has an  n -point tree.The nodes are numbered from  0  to  n1 ,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  n1  integers  fa[1]fa[n1] ,which describe the father of nodes  1n1 (node  0  is the root).It is guaranteed that  0fa[i]<i .

The next line contains  n  integers  a[0]a[n1] ,which describe the initial stones on each nodes.It is guaranteed that  0a[i]<134217728 .

1T100 , 1n100000 .

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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5998  5997  5994  5993  5992 
题意:有一棵树,树的每个节点上都有一定数量的石子,现在给出每个节点的父亲节点,要求:每次可以将节点的石子移动到其父亲节点上(移动的石子数不能为0),当不能操作时输:
思路:DFS求每个节点的深度,对深度为奇数的进行异或;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>g[100100];
int num[100100];
int  depth[100100];
int ans,n;
void dfs(int i,int d)
{
    if(d&1)
    ans^=num[i];
    for(int j=0;j<g[i].size();j++)
    {
    	depth[g[i][j]]=depth[i]+1;
    	dfs(g[i][j],depth[g[i][j]]);
	}
}
int main()
{
	int t,i,j,m,x;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			g[i].clear();
			depth[i]=0;
		}
		for(i=1;i<n;i++)
		{
			scanf("%d",&x);
			g[x].push_back(i);
		}
		for(i=0;i<n;i++)
		{
			scanf("%d",&num[i]);
		}
		ans=0;
		dfs(0,0);
		if(ans)
		printf("win\n");
		else
		printf("lose\n");
	}
	return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值