UVA 11927 - Games Are Important(sg函数)
UVA 11927 - Games Are Important
题意:给定一个有向图,结点上有一些石头,两人轮流移动石头。看最后谁不能移动就输了,问先手还后手赢
思路:求出每一个结点的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函数)的更多相关文章
- UVA - 11927 Games Are Important (SG)
Description Games Are Important One of the primary hobbies (and research topics!) among Computing ...
- UVA 10561 - Treblecross(博弈SG函数)
UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...
- UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)
UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO.最后一个放的人赢.问谁赢 思路:sg函数.每一段.. ...
- hdoj 1729 Stone Games(SG函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729 看了题目感觉像Nim,但是有范围限制,有点不知道SG函数该怎么写 看了题解,最后才明白该怎么去理 ...
- UVa 10561 Treblecross (SG函数)
题意:给定上一行字符串,其中只有 X 和 . 并且没有连续的三个 X,两个玩家要分别在 . 上放 X,如果出现三个连续的 X,则该玩家胜利,现在问你先手胜还是败,如果是胜则输出第一步可能的位置. 析: ...
- (转)博弈 SG函数
此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------- ...
- 【转】博弈—SG函数
转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...
- hdu1536&&hdu3023 SG函数模板及其运用
S-Nim Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status ...
- LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
G - Game of Hyper Knights Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
随机推荐
- 第3节 mapreduce高级:10、11、分组求取topN
只要修改OrderReducer.java的reduce方法,修改为: int i = 0;for(NullWritable nullWritable:values){ if(i>=2) bre ...
- pip install MySQL-python error "can't open config-win.h"
http://blog.csdn.net/xxm524/article/details/48754139
- python 2 encode and decode
https://docs.python.org/2/howto/unicode.html a Unicode string is a sequence of code points, which ar ...
- 笔试算法题(11):Josephus环 & Fibonacci序列
出题:Josephus Cycle,约瑟夫环问题.k个数字连成一个环,第一个数字为1.首先从1开始计数删除第m个数字:然后从上次被删除的数字的下一个数字开始计数,删除第m个数字:重复进行第二步直到只剩 ...
- Shiro框架 (原理分析与简单实现)
Shiro框架(原理分析与简单实现) 有兴趣的同学也可以阅读我之前分享的:Java权限管理(授权与认证)CRM权限管理 (PS : 这篇博客里面的实现方式没有使用框架,完全是手写的授权与认证,可以 ...
- Excel OLE控制
来源:http://blog.csdn.net/chelen_jak/article/details/7069095 Delphi 控制Excel(一) 使用动态创建的方法 首先创建 Excel 对 ...
- api安全认证
三.auth自定义授权 客户端代码: import requests import hashlib import time current_time = time.time() #自意义的字符串app ...
- hihoCoder #1162 : 骨牌覆盖问题·三
#1162 : 骨牌覆盖问题·三 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 前两周里,我们讲解了2xN,3xN骨牌 ...
- [luoguP3572] [POI2014]PTA-Little Bird(DP + 单调队列)
传送门 DP方程 f[i] = f[j] + (a[j] <= a[i]) ( i - k < j < i ) 要使 f[i] 最小,需要等号后面的值最小,可以用单调队列来维护. 至 ...
- vim fulerformat的设置
在vim中设置选项,有注释很容易明白: set laststatus=1 "2总显示最后一个窗口的状态行,1窗口多于一个时显示最后一个窗口的状态行,0不显示最后一个窗口的状态行 fulerf ...