UVA-11927 Games is Important (SG)

Source: Internet
Author: User

Description


Games is Important
One of the primary hobbies (and topics!) among Computing science students at the University of Alberta are, of cou RSE, the playing of games. People here as playing games very much, but the problem was that the games could get solved completely--as happened in the Case of checkers. Generalization of games are the only hope and worries that they would be solved linger still. Here's an example of a generalization of a and a-B player game which can also be solved.

Suppose we have a directed acyclic graph with some number of stones at each node. 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 is 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 the integers Nand mThe number of nodes and the number of edges respectively. (1 N,0 m10000). Then, mLines follow, each containing, integers aand b: The starting and ending node of the edge (nodes is labeled from 0 to N -1).

The test case was terminated by n more integerss0,..., sn-1 (one per line), WHERE sI represents the number of stones that is initially placed on node i ( 0si10 XX).

Each test case was followed by a blank line, and input was terminated by a line containing '0 0' which should not Be processed.

OutputFor each test case output a single line with either the word ' First' If the first player would win, or the word 'Second' If the second player would 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 a five-ring graph). There are a few stones on each node.

Two players take turns to move a stone from one node to the adjacent node along a random line starting from the point. Players who cannot move are counted as losing the game. Notice that at the same moment a node can have random stones.

Idea: Note that the state of each stone is completely independent, so this game can be seen as the game of each stone.

For each stone, its state x is where the node number, assuming that the node has no starting edge, it is the state of the initiator will be defeated, otherwise the state is the adjacent node of the SG value set.

It is important to note that for the same node. The number of stones on the assumption is odd. You can do it as a 1 stone. Assumed to be even, negligible. This is determined by the nature of the XOR operation.

#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 is Important (SG)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.