UV-11927 Games Are Important (SG)
Description
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 neighbors, 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.
InputThe input consists of a number of test cases. Each test case begins with a line containing two integers
NAnd
M, The number of nodes and the number of edges respectively. (1
N1000, 0
M10000). Then,
MLines follow, each containing two integers
AAnd
B: The starting and ending node of the edge (nodes are labeled from 0
N-1 ).
The test case is terminatedNMore integersS0 ,...,SN-1 (one per line), whereSI represents the number of stones that are initially placed on nodeI(0Si1000 ).
Each test case is followed by a blank line, and input is terminated by a line ining'0 0'Which shoshould not be processed.
OutputFor 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 30 11 22 310007 70 10 20 42 34 55 64 310101000 0
Sample Output
FirstSecond has a DAG (directed to the five rings), and each node has some stones. The two players take turns to move a stone from a node to any directed edge starting from this point to the adjacent node. Players that cannot be moved will lose the game. Note that there can be any stone on a node at the same time. Thought: I noticed that the status of each stone is completely independent, so this game can be seen as the sum of the game formed by each stone. For each stone, its State x is the node number where it is located. If there is no starting edge at this node, it is both a first-hand defeat, otherwise, the subsequent State is the SG value set of the adjacent node.
It should be noted that, for the same node, if the number of stones on it is an odd number, it can be regarded as one stone; if it is an even number, it can be ignored. This is determined by the nature of the exclusive or operation.
#include
#include
#include
#include #include
using namespace std;const int maxn = 10005;int n, m, sg[maxn];vector
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;}