多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > UVA - 11927 Games Are Important (SG)

UVA - 11927 Games Are Important (SG)

來源:程序員人生   發布時間:2014-09-16 14:01:29 閱讀次數:3272次

Description

Download as PDF


  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.
epsfbox{p11927.eps}

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. ( 1$ le$n$ le$1000, 0$ le$m$ le$10000). 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 ( 0$ le$si$ le$1000).

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 ", ans ? "First": "Second"); } return 0; }



生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 一级片大全 | 五月婷婷视频在线观看 | 午夜影院免费体验 | 亚洲精品福利 | 激情的网站 | 日韩一级视频免费观看 | 日本午夜理伦三级在线观看 | 亚洲图片一区二区三区 | 亚洲国产成人资源在线软件 | 2019免费视频 | 真实呦女free性 | 日本最新伦中文字幕 | 久久都是精品 | 毛片免费永久不卡视频观看 | 老女人毛片| 国产一级做a爰大片免费久久 | 成在线人免费视频 | 黑人猛操| 国产精品久久毛片 | 日韩字幕无线乱码 | 国产欧美精品国产国产专区 | 亚洲高清在线观看看片 | 欧美成人看片一区二区三区 | 欧美性受一区二区三区 | 国产精品公开免费视频 | 亚洲视频二 | 永久在线观看视频 | 亚洲精品在线播放视频 | 亚洲精品区一区二区三区四 | 大片免费在线观看网址 | 国产精品精品国产一区二区 | 国产极品美女在线观看 | 国产免费资源高清小视频在线观看 | 日韩欧美一区二区中文字幕 | 国产在线精品一区二区高清不卡 | 亚洲免费高清视频 | 中文字幕一区2区 | 片在线观看免费观看视频 | 亚洲精品日韩在线一区 | 精品国产乱码久久久久久一区二区 | 日韩成a人片在线观看日本 日韩成人国产精品视频 |