[2016北京集训试题6]魔法游戏-[博弈论-sg函数]

Description

Solution

首先,每个节点上的权值可以等价于该节点上有(它的权的二进制位数+1)个石子,每次可以拿若干个石子但不能不拿。

然后就发现这和NIM游戏很像,就计算sg函数em(然而我并不会推)

如果您恰好看到这篇博,又恰好有空的话,欢迎探讨~

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef unsigned long long ull;
int n,x,y;
int num[100010];
ull t;
struct G{int y,nxt;}g[200010];int h[100010],tot=0;
int dfs(int x,int fa)
{
    int c=num[x],d=0;
    for (int i=h[x];i;i=g[i].nxt)
        if (g[i].y!=fa) d^=dfs(g[i].y,x);
    return c-=(c<=d);
}
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        for (int i=1;i<=n;i++){scanf("%llu",&t);num[i]=(int)log2(t)+1;}
        memset(h,0,sizeof(h));tot=0;
        for (int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);x++;y++;
            g[++tot]=G{y,h[x]};h[x]=tot;
            g[++tot]=G{x,h[y]};h[y]=tot;
        }
        dfs(1,0)?printf("Alice\n"):printf("Marisa\n");      
    }
}

猜你喜欢

转载自www.cnblogs.com/coco-night/p/9690925.html