UVA 11927 - Games Are Important

option=com_onlinejudge&Itemid=8&page=show_problem&category=478&problem=3078&mosmsg=Submission+received+with+ID+13891171" target="_blank" style="">题目链接

题意:给定一个有向图,结点上有一些石头,两人轮流移动石头。看最后谁不能移动就输了,问先手还后手赢

思路:求出每一个结点的sg函数,然后偶数个石头结点能够不用考虑,由于对于偶数情况,总步数肯定能保证是偶数,所以仅仅要考虑奇数情况的结点

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std; const int N = 1005;
int n, m, sg[N];
vector<int> g[N]; int dfs(int u) {
if (sg[u] != -1) return sg[u];
if (g[u].size() == 0) return sg[u] = 0;
bool vis[N];
memset(vis, false, sizeof(vis));
for (int i = 0; i < g[u].size(); i++)
vis[dfs(g[u][i])] = true;
for (int i = 0; ; i++)
if (!vis[i]) return sg[u] = i;
} int main() {
while (~scanf("%d%d", &n, &m) && n || m) {
int u, v;
memset(g, 0, sizeof(g));
memset(sg, -1, sizeof(sg));
while (m--) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
}
for (int i = 0; i < n; i++)
dfs(i);
int ans = 0, num;
for (int i = 0; i < n; i++) {
scanf("%d", &num);
if (num&1)
ans ^= sg[i];
}
printf("%s\n", ans == 0? "Second":"First");
}
return 0;
}

UVA 11927 - Games Are Important(sg函数)的更多相关文章

  1. UVA - 11927 Games Are Important (SG)

    Description  Games Are Important  One of the primary hobbies (and research topics!) among Computing ...

  2. UVA 10561 - Treblecross(博弈SG函数)

    UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...

  3. UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)

    UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO.最后一个放的人赢.问谁赢 思路:sg函数.每一段.. ...

  4. hdoj 1729 Stone Games(SG函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729 看了题目感觉像Nim,但是有范围限制,有点不知道SG函数该怎么写 看了题解,最后才明白该怎么去理 ...

  5. UVa 10561 Treblecross (SG函数)

    题意:给定上一行字符串,其中只有 X 和 . 并且没有连续的三个 X,两个玩家要分别在 . 上放 X,如果出现三个连续的 X,则该玩家胜利,现在问你先手胜还是败,如果是胜则输出第一步可能的位置. 析: ...

  6. (转)博弈 SG函数

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

  7. 【转】博弈—SG函数

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

  8. hdu1536&&hdu3023 SG函数模板及其运用

    S-Nim Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status ...

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

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

随机推荐

  1. HR教你面试时怎么谈出高工资

    不是任何时候谈钱都会伤感情,比如跟客户谈合同报价,跟房东谈房租,以及面试时和公司HR谈新工作的薪酬待遇. 这事儿一般不需要你先开口.在面试进入尾声的时候,如果HR对你还算满意,通常就会开始问你目前的薪 ...

  2. win下配置qt creator 能够执行c/c++

    首先需要相关包共四个: qt-win-opensource-4.8.5-mingw.exe qt-creator-windows-opensource-2.8.1.exe MinGW-gcc440_1 ...

  3. 关于Integer,127和128的问题

    里面的,直接贴源码来看 Integer i=127; Integer b=128; Integer c=128; Integer d=127;Integer j;System.out.println( ...

  4. 页面jsp向后端发送:HTTP 400错误 - 请求无效(Bad request)

    HTTP 400错误 - 请求无效(Bad request) jsp页面有误 在ajax请求后台数据时有时会报 HTTP 400 错误 - 请求无效 (Bad request);出现这个请求无效报错说 ...

  5. js 技巧 (五)

    //设置光标位置 function getCaret(textbox) { var control = document.activeElement; textbox.focus(); var ran ...

  6. 【数据库】DML-增删改查-SQL实现

    一.数据插入-Insert 1. 插入单条记录 insert into 表名(字段名,字段名,字段名) //当插入所有字段时,字段名可以省略 values('值1','值2','值3'); 2. 插入 ...

  7. Laravel核心解读 -- 扩展用户认证系统

    扩展用户认证系统 上一节我们介绍了Laravel Auth系统实现的一些细节知道了Laravel是如何应用看守器和用户提供器来进行用户认证的,但是针对我们自己开发的项目或多或少地我们都会需要在自带的看 ...

  8. 基于nvm的Node、NPM的版本管理(NPM permission error的解决)

    最近在使用npm过程中,发现全局安装总会遇到permission相关的错误,所以总是要在前面加sudo,还得不停输入密码. 懒惰使我进步,随手google了一下相关问题的解决方案,发现npm在官方文档 ...

  9. add list of symbols -- latex

    * add list of symbols -- latexinclude a *toc.tex* file in the *main.tex* in *main.tex*#+BEGIN_SRC la ...

  10. 【BZOJ 1202】 [HNOI2005]狡猾的商人(枚举区间也可行)

    题链:http://www.lydsy.com/JudgeOnline/problem.php?id=1202 其实也可以不使用加权并查集,通过画图可以发现,一个长区间和其包含的区间能够算出一个新区间 ...