HDU 5996 dingyeye loves stone [阶梯Nim]

dingyeye喜欢和你玩石子游戏。

dingyeye有一棵nn个节点的有根树,节点编号为00到n−1n−1,根为00号节点。游戏开始时,第ii个节点上有a[i]a[i]个石子。两位玩家轮流操作,每次操作玩家可以选择一个节点,并将该节点上的一些石子(个数不能为00)移动到它的父亲节点上去。如果轮到某位玩家时,该玩家没有任何合法的操作可以执行,则判负。

你在游戏中执先手,你想知道当前局面你能否必胜。

裸题...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=1e5+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
int n,m;
struct edge{int v,ne;}e[N<<1];
int cnt=1,h[N];
inline void ins(int u,int v){
    e[++cnt]=(edge){v,h[u]};h[u]=cnt;
}
int deep[N];
void dfs(int u){
    for(int i=h[u];i;i=e[i].ne)
        deep[e[i].v]=deep[u]+1,dfs(e[i].v);
}
int main(){
    freopen("in","r",stdin);
    int T=read();
    while(T--){
        n=read();
        cnt=0;memset(h,0,sizeof(h));
        for(int i=2;i<=n;i++) ins(read()+1,i);
        dfs(1);
        int sg=0,a;
        for(int i=1;i<=n;i++) a=read(),sg^=deep[i]&1 ? a : 0;
        puts(sg ? "win" : "lose");
    }
}
时间: 2024-12-19 05:20:08

HDU 5996 dingyeye loves stone [阶梯Nim]的相关文章

hdu 5996 dingyeye loves stone(博弈)

题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你当前的局面,问你能否赢 题解: 设根节点的深度为0,将所有深度为奇数的节点的石子数目xor起来,则先手必胜当且仅当这个xor和不为0. 证明同阶梯博弈.对于偶深度的点上的石子,若对手移动它们,则可模仿操作:对于奇深度上的石子,移动一次即进入偶深度的点. 时空复杂度O(n). 1 #include<b

HDU 5996 dingyeye loves stone ---BestCoder Round #90

题目链接 设根节点的深度为0,将所有深度为奇数的节点的石子数目xor起来,则先手必胜当且仅当这个xor和不为0. 证明同阶梯博弈.对于偶深度的点上的石子,若对手移动它们,则可模仿操作:对于奇深度上的石子,移动一次即进入偶深度的点. 时空复杂度O(n). 用vector存搜一下就行. #include <cstdio> #include <iostream> #include <vector> #include <cstring> using namespac

HDU5996:dingyeye loves stone

题目链接:dingyeye loves stone 题意:给出一棵树,树上的每个节点都有石子若干, 两人博弈,每次操作都可以把任意节点的任意石子数转移到它的父亲节点, 若无法操作则输,给出树上的节点及石子数,问先手是否能赢 分析:“阶梯博弈”,若深度为偶数的节点,对方移多少石子,我们就移多少.故偶数节点可忽略不计 接下来只要异或深度为奇数的节点石子数即可 感想:深度不会求,dfs不会写,我真菜 第一种方法. 记录每个节点的子节点,dfs遍历所有节点,若深度为奇则异或 1 #include<cst

HDU 5996:dingyeye loves stone(阶梯博弈)

http://acm.hdu.edu.cn/showproblem.php?pid=5996 题意:在一棵树上进行博弈,每次只能将当前的结点的石子放到父节点上,最后不能移动的输. 思路:比赛的时候想的是对于每一个深度为dep的结点,可以转化为dep堆同样的深度为1的结点,然后就不会了,忘了最后异或起来偶数可以抵消,相当于对方移动,我方也跟着移动对方上一个回合移动的石子,所以最后只要考虑深度为奇数的结点. 1 #include <cstdio> 2 #include <algorithm&

HDU 4315 Climbing the Hill [阶梯Nim]

传送门 题意: 和上题基本一样:山顶可以有多人,谁先把king放到山顶谁就胜 并不太明白 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; const int N=1005; inline int read(){ char

[ACM] hdu 4248 A Famous Stone Collector (DP+组合)

A Famous Stone Collector Problem Description Mr. B loves to play with colorful stones. There are n colors of stones in his collection. Two stones with the same color are indistinguishable. Mr. B would like to select some stones and arrange them in li

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[

POJ 1704 Georgia and Bob [阶梯Nim]

题意: 每次可以向左移动一个棋子任意步,不能跨过棋子 很巧妙的转化,把棋子间的空隙看成石子堆 然后裸阶梯Nim #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; const int N=1005; inline int re

hdu 5677 ztr loves substring 二维费用背包+回文

题目链接: hdu 5677 ztr loves substring 官方题解: //这部分是错的(首先,对于每一个串i跑一次manancher,令g[i][j]=p[j]-1g[i][j]=p[j]−1 这样,g就存储了所有的回文子串的长度 为了方便,把g降到一维表示) 首先,因为字符串长度较小,所以直接二重for循环找就好了,用一个数组 g记录各个回文串的长度 不妨把每一个子串抽象成一个物品 费用为二维的,即{长度,1} 价值是Bool型的 这样就成了一个二维判断可行性费用背包问题 设f(i