【hdu 5996】dingyeye loves stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 208 Accepted Submission(s): 119
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] < 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
【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=5996
【题解】
可以转化为阶梯博弈;
对深度为偶数的节点进行操作;
相当于一个NIM博弈;
把石子扔掉;
(如果对方对深度为奇数的节点操作,那么你总能把对方扔到偶数深度节点上的石头等量地移动到深度为奇数的节点上);
始终保持偶数深度上节点的石头为你的必胜态(你)或必败态(对方);
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int MAXN = 100000+100;
LL a[MAXN],dd;
int n,totm;
int fir[MAXN],nex[MAXN*2],en[MAXN*2];
void dfs(int x,int ndep)
{
if (ndep%2==0)
dd^=a[x];
for (int temp = fir[x];temp;temp = nex[temp])
{
int y = en[temp];
dfs(y,ndep+1);
}
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
int T;
rei(T);
while (T--)
{
totm = 0;
rei(n);
rep1(i,0,n-1)
fir[i] = 0;
rep1(i,1,n-1)
{
int fa;
rei(fa);
nex[++totm] = fir[fa];
fir[fa] = totm;
en[totm] = i;
}
rep1(i,0,n-1)
rel(a[i]);
dd = 0;
dfs(0,1);
if (dd!=0)
puts("win");
else
puts("lose");
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 上周热点回顾(1.20-1.26)