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. ansible基础配置

    1.基础配置 1.1.环境 主机配置 ansible版本:2.7.4 控制端:centos7.4,IP:192.168.1.213,主机名:operation 被控制端: centos6.5,IP:1 ...

  2. 使用Hashids来保护你的数据库主键

    为什么要保护数据库主键? 数据库主键一般是有序自增主键,极易被爬虫抓取数据,作为应用开发者,这是不应该的,你辛辛苦苦收集的数据转眼之间被其他人给抓取了,是不是很大的损失? Hashids的介绍 gen ...

  3. LeetCode (45) Jump Game II

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  4. Python基础之列表、元组、字典、集合的使用

    一.列表 1.列表定义 names=["Jhon","Lucy","Michel","Tom","Wiliam ...

  5. SSM java.lang.NullPointerException

    如上图所示的异常 原来是少了这个东西,加上就好了

  6. Webdriver概述(selenium对应浏览器版本)

    Webdriver (Selenium2)是一种用于Web应用程序的自动测试工具,它提供了一套友好的API,与Selenium 1(Selenium-RC)相比,Webdriver 的API更容易理解 ...

  7. 光纤通信(codevs 1955)

    题目描述 Description 农民John 想要用光纤连通他的N (1 <= N <= 1,000)个牲口棚(编号1..N).但是,牲口棚位于一个大池塘边,他仅可以连通相邻的牲口棚.J ...

  8. android开发里跳过的坑——button不响应点击事件

    昨天遇到一个头疼的问题,在手机上按钮事件都很正常,但是在平板上(横屏显示的状态),button点击事件不响应,代码简化如下: public class Test extends Activity im ...

  9. 【Github】如何删除github上的项目

    1.登录你的githup账户,进入到仓库页面如下图 2.点击setting进入到该仓库的设置界面 3.复制一下仓库的名称,然后下拉到最后,点击delete this repository 4.将刚刚复 ...

  10. Codeforces 631A Interview【模拟水题】

    题意: 模拟模拟~~ 代码: #include<iostream> using namespace std; const int maxn = 1005; int a[maxn], b[m ...