zoukankan      html  css  js  c++  java
  • [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
    "):printf("Marisa
    ");      
        }
    }
  • 相关阅读:
    Python面向对象的魔术方法
    Python面向对象基础
    Python异常处理
    Python装饰器实现函数动态类型检查
    Python装饰器
    Python IO
    HTTP协议
    应用层常用协议
    读写分离
    MySQL优化三之MySQL配置
  • 原文地址:https://www.cnblogs.com/coco-night/p/9690925.html
Copyright © 2011-2022 走看看