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;
}

时间: 2024-08-01 06:19:42

UVA - 11927 Games Are Important (SG)的相关文章

UVA 11927 - Games Are Important(sg函数)

UVA 11927 - Games Are Important 题目链接 题意:给定一个有向图,结点上有一些石头,两人轮流移动石头,看最后谁不能移动就输了,问先手还后手赢 思路:求出每个结点的sg函数,然后偶数个石头结点可以不用考虑,因为对于偶数情况,总步数肯定能保证是偶数,所以只要考虑奇数情况的结点 代码: #include <stdio.h> #include <string.h> #include <algorithm> #include <vector&g

uva 11927 - Games Are Important(组合游戏+记忆化)

题目链接:uva 11927 - Games Are Important 题目大意:给出一张无环有向图,并给出每个节点上的石子数,每次操作可以选择一个石子,向下一个节点移动.两人轮流操作,直到不能操作为失败者. 解题思路:有了图之后,用记忆化的方式处理出每个节点的SG值,取所有石子数为奇数的节点的Nim和. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; con

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

UVA 1482 - Playing With Stones(SG打表规律)

UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个,不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值很大,无法直接递推sg函数,打出前30项的sg函数找规律 代码: #include <stdio.h> #include <string.h> int t, n; long long num; long long SG(long long x) { return x % 2 == 0 ? x : SG(x /

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

UNDERSTANDING THE GAUSSIAN DISTRIBUTION

UNDERSTANDING THE GAUSSIAN DISTRIBUTION Randomness is so present in our reality that we are used to take it for granted. Most of the phenomena which surround us have been generated by random processes. Hence, our brain is very good at recognise these

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

UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO,最后一个放的人赢,问谁赢 思路:sg函数,每一段...看成一个子游戏,利用记忆化求sg值,记忆化的状态要记录下左边和右边是X还是O即可 代码: #include <stdio.h> #include <string.h> const int N = 105; int t, sg[3][3][N]; char str[N]; int ge

UVA - 10561 Treblecross (SG定理)

Treblecross is a two player gamewhere the goal is to get three X in a row on a one-dimensional board. At the startof the game all cells in the board is empty. In each turn a player puts a X in an empty cell, and if that results in there beingthree X

hdoj 1729 Stone Games(SG函数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729 看了题目感觉像Nim,但是有范围限制,有点不知道SG函数该怎么写 看了题解,最后才明白该怎么去理解 . 首先进行对s和c进行分类, 1.c = 0 的时候,无论怎样都填不满,直接跳过: 2.c = s 的时候,先手必败,即是P态: 3.c < s 的时候,可以分为两种情况: 1)c^2 + c < s 的时候,递归 2)c^2 + c > s 的时候,先手必胜,即N态 1 int me