SG函数:

对于任意状态,定义SG(x)=mex(S),其中S是x的后继状态的SG函数值集合,mex(S)表示不再S内的最小非负整数

SG(X)=0当且仅当x为必败态。

解:

构造一个有向无环图(树),SG(x)=mex(SG(y)) y为x的孩子节点。

SG(所有叶子节点)=0

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
#define MAXN 10005
int n;
int sg[MAXN];
vector <int >map[MAXN];
int vis[MAXN];
void dfs(int y)
{
if (vis[y]) return;
int i,j;
int x;
vector<int> tmp;
for (j=;j<map[y].size();j++)
{
dfs(map[y][j]);
tmp.push_back(sg[map[y][j]]);
}
sort(tmp.begin(),tmp.end());
x=;
int flag=true;
for (i=;i<tmp.size();i++)
if (tmp[i]==x)
x++;
else
{
if (tmp[i]>x)
{
break;
}
}
sg[y]=x;
vis[y]=;
}
int main()
{
int i,j,m,x;
int dot[MAXN];
int cas=;
while (scanf("%d",&n)!=EOF)
{
printf("Case %d:\n",cas++);
for (i=;i<=n;i++)
map[i].clear();
memset(sg,,sizeof(sg));
memset(vis,,sizeof(vis));
for (i=;i<n;i++)
{
scanf("%d",&dot[i]);
for (j=;j<dot[i];j++)
{
scanf("%d",&x);
map[i].push_back(x);
}
}
sg[n]=;
for (i=;i<=n;i++)
if (!vis[i])
dfs(i);
int q;
int p;
scanf("%d",&q);
for (i=;i<q;i++)
{
scanf("%d",&p);
int ans=;
for (j=;j<p;j++)
{
scanf("%d",&x);
ans^=sg[x];
}
if (ans==)
printf("Bob\n");
else
printf("Alice\n");
}
}
return ;
}

ZOJ 3666 博弈 SG函数的更多相关文章

  1. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

  2. hdu 3032(博弈sg函数)

    题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...

  3. HDU-4678 Mine 博弈SG函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4678 题意就不说了,太长了... 这个应该算简单博弈吧.先求联通分量,把空白区域边上的数字个数全部求出 ...

  4. (转)博弈 SG函数

    此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------- ...

  5. 尼姆博弈+SG函数

    博弈这个东西真的很费脑诶.. 尼姆博奕(Nim Game):游戏者轮流从一堆棋子(或者任何道具)中取走一个或者多个,最后不能再取的就是输家.当指定相应数量时,一堆这样的棋子称作一个尼姆堆 当n堆棋子的 ...

  6. 【转】博弈—SG函数

    转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...

  7. HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)

    Fibonacci again and again Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  8. Light OJ 1199 - Partitioning Game (博弈sg函数)

    D - Partitioning Game Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

    G - Game of Hyper Knights Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

随机推荐

  1. 勇者斗恶龙 UVA 11292

    Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shor ...

  2. 基于XML文档的声明式事务配置

    <!-- 配置事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.t ...

  3. 关于pom.xml中的dependency中的顺序

    特别注意: <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>l ...

  4. Leetcode 93.复制IP地址

    复制IP地址 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135& ...

  5. [poj3537]Crosses and Crosses_博弈论

    Crosses and Crosses poj-3537 题目大意:给定一个1*n的网格,每次往格子内填一个$\times$,连续的三个即可获胜. 注释:$1\le n\le 2000$. 想法:我们 ...

  6. P1656 炸铁路 洛谷

    https://www.luogu.org/problem/show?pid=1656 题目描述 因为某国被某红色政权残酷的高压暴力统治.美国派出将军uim,对该国进行战略性措施,以解救涂炭的生灵. ...

  7. Ubuntu 16.04设置文件夹试图永久以列表显示

    图片来自:http://forum.ubuntu.org.cn/viewtopic.php?p=2043385

  8. Spring MVC 4实现RESTFul WebServices的CRUD实例和使用RestTemplate进行请求(全注解形式配置Web和Filter)

    在这篇文章中,我们将使用Spring4 MVC编写一个CRUD RESTful Web服务,写一个REST客户端RestTemplate来使用这些服务.我们也将利用外部客户端测试的服务. 下面将展示核 ...

  9. vue2的简单Popup (Confirm,Alert)组件

    github:  https://github.com/longfei59418888/vui   (记得给一个 start,以后有一起讨论,各种好组件) demo :http://60.205.20 ...

  10. 不折移动web不腾--开启我的个人Mac之旅

    背景,非常久非常久曾经(听过)Linux,瞎玩 Mac mini,而今Linux下开发技能半身不遂,处于放任状态.明明就知道随着时间流逝会越陌生的东西越不想去抓住最后的余温,不知道这算不算放弃,反正迟 ...