HDOJ 5996 dingyeye loves stone(階梯博弈)

dingyeye loves stone

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


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
 


題意:請戳:中文題面 


題解:階梯博弈裸題啊, 可是沒學過,卒。。 請戳→ 階梯博弈   寫的挺詳細的一篇博文,有幾點筆誤。 但是很清楚的講了奇偶性對整體的影響及證明。。看看就懂了


代碼如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10; 
int d[maxn],fa[maxn];

int dfs(int x)//按照題意fa[a]<a,也可以不用這麼找深度 
{
	if(d[x]!=-1)
		return d[x];
	return d[x]=dfs(fa[x])+1;
}

int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<n;++i)
			scanf("%d",&fa[i]);
		memset(d,-1,sizeof(d));
		d[0]=0;
		for(int i=1;i<n;++i)
		{
			if(d[i]==-1)
				d[i]=dfs(i);
		}
		int ans=0,num;
		for(int i=0;i<n;++i)
		{
			scanf("%d",&num);
			if(d[i]&1)
				ans^=num;
		}
		if(ans)
			printf("win\n");
		else
			printf("lose\n");
	}
	return 0;
} 




發佈了643 篇原創文章 · 獲贊 225 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章