UVA - 11927 Games Are Important (SG)
Description
Games Are Important |
One of the primary hobbies (and research topics!) among Computing Science students at the University of Alberta is, of course, the playing of games. People here like playing games very much, but the problem is that the games may get solved completely--as happened
in the case of Checkers. Generalization of games is the only hope, but worries that they will be solved linger still. Here is an example of a generalization of a two player game which can also be solved.
Suppose we have a directed acyclic graph with some number of stones at each node. Two players take turns moving a stone from any node to one of its neighbours, following a directed edge. The player that cannot move any stone loses the game. Note that multiple
stones may occupy the same node at any given time.
Input
The input consists of a number of test cases. Each test case begins with a line containing two integers
n and m, the number of nodes and the number of edges respectively. (
1n1000,
0m10000).
Then, m lines follow, each containing two integers
a and b: the starting and ending node of the edge (nodes are labeled from 0 to
n - 1).
The test case is terminated by n more integers
s0,..., sn-1 (one per line), where
si represents the number of stones that are initially placed on node
i ( 0si1000).
Each test case is followed by a blank line, and input is terminated by a line containing `0 0' which should not be processed.
Output
For each test case output a single line with either the word ` First' if the first player will win, or the word `
Second' if the second player will win (assuming optimal play by both sides).
Sample Input
4 3
0 1
1 2
2 3
1
0
0
0 7 7
0 1
0 2
0 4
2 3
4 5
5 6
4 3
1
0
1
0
1
0
0 0 0
Sample Output
First
Second
有一个DAG(有向五环图)。每一个结点上都有一些石子。 两个玩家轮流把一个石头从一个结点沿着从此点出发的随意一条有向边移向相邻结点。不能移动的玩家算输掉游戏。注
意,在同一个时刻一个节点上能够有随意的石头。 思路:注意到,各个石头的状态的是全然独立的,所以这个游戏能够看做每个石头所形成的游戏的和。 对于每个石头,它的状态x就是所在的结点编号,假设此结点已经没有出发的边,则既是先手必败的状态,否则兴许状态就是相邻结点的SG值集合。 须要注意的是,对于在同一个结点来说。其上的石头假设个数为奇数。则当成1个石头就可以。假设为偶数,能够忽略不计。这是由异或运算的性质决定的。#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 10005; int n, m, sg[maxn];
vector<int> g[maxn]; int SG(int u) {
if (sg[u] != -1)
return sg[u]; int vis[maxn];
memset(vis, 0, sizeof(vis));
for (int i = 0; i < g[u].size(); i++) {
int tmp = SG(g[u][i]);
vis[tmp] = 1;
} for (int j = 0; ; j++)
if (!vis[j]) {
sg[u] = j;
break;
}
return sg[u];
} int main() {
int u, v;
while (scanf("%d%d", &n, &m) != EOF && n+m) {
memset(sg, -1, sizeof(sg));
for (int i = 0; i < maxn; i++)
g[i].clear(); for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
} for (int i = 0; i < n; i++)
sg[i] = SG(i); int ans = 0, u;
for (int i = 0; i < n; i++) {
scanf("%d", &u);
if (u & 1)
ans ^= sg[i];
}
printf("%s\n", ans ? "First": "Second");
}
return 0;
}
UVA - 11927 Games Are Important (SG)的更多相关文章
- UVA 11927 - Games Are Important(sg函数)
UVA 11927 - Games Are Important option=com_onlinejudge&Itemid=8&page=show_problem&catego ...
- UVA 1482 - Playing With Stones(SG打表规律)
UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个.不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值非常大.无法直接递推sg函 ...
- UVA 10561 - Treblecross(博弈SG函数)
UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...
- Inside NGINX: How We Designed for Performance & Scale
NGINX leads the pack in web performance, and it’s all due to the way the software is designed. Where ...
- UNDERSTANDING THE GAUSSIAN DISTRIBUTION
UNDERSTANDING THE GAUSSIAN DISTRIBUTION Randomness is so present in our reality that we are used to ...
- 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 (SG函数 递推) Treblecross
如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...
- uva 1378 A Funny Stone Game (博弈-SG)
题目链接:http://vjudge.net/problem/viewProblem.action?id=41555 把第i堆的每个石子看出一堆个数为n-i的石子,转换为组合游戏 #include & ...
随机推荐
- Django基础三之视图函数
一 Django的视图函数view 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...
- 慕课网 javascript深入浅出编程练习
任务 请在index.html文件中,编写arraysSimilar函数,实现判断传入的两个数组是否相似.具体需求: 1. 数组中的成员类型相同,顺序可以不同.例如[1, true] 与 [false ...
- Vue 实现复制到粘贴板功能
vue 实现复制到粘贴板功能需要依赖到 clipboard.js 1. 首先需要安装依赖 * 出现错误的话,可以试试 cnpm npm install --save vue-clipboard2 2 ...
- javascript的继承实现
javascript虽然是一门面向对象的语言,但是它的继承机制从一开始设计的时候就不同于传统的其他面向对象语言,是基于原型的继承机制,但是在这种机制下,继承依然有一些不同的实现方式. 方法一:类式继承 ...
- Android批量打包提速 - 1分钟900个市场不是梦
版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4152323.html 黎明前的黑暗 使用Ant或者Gradl ...
- Linux安装Tomcat服务器发布项目教程
前面小Alan跟大家聊了在Linux服务器上jdk运行环境的安装以及redis非关系型数据库的安装,今天继续跟大家聊聊Tomcat的安装,以及将我们的项目发布上去并成功的访问. 第一步:将tomcat ...
- 结对编程的感想&收获
关于结对编程的感想.感受,见我的另一篇随笔——<构建之法>结对编程 感想 下面我来谈谈本次结对编程的收获以及发现的问题 收获 ①这是我人生中第一次做UI界面设计,刚拿到这个题目还是比较 ...
- 网站源IP暴露使用高防之后还行不行如何解决?
如题:使用高防后源站IP暴露的解决办法 在购买高防IP后,如果还存在攻击绕过高防直接打到源站IP的情况,就需要更换下源站IP了.但在这之前,请务必排查确认没有其他可能暴露源站IP的因素后,再去更换源站 ...
- [翻译] MotionBlur
MotionBlur https://github.com/fastred/MotionBlur MotionBlur allows you to add motion blur effect to ...
- 给category添加基本数据类型属性
给category添加基本数据类型属性 说明 通常,我们添加属性都是通过对象扩展来实现的,其实,我们也可以用runtime来添加基本数据类型的属性 源码 // // UIView+AnimationP ...