A Chess Game
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 3551 |
|
Accepted: 1440 |
Description
Let ' s design a new chess game. There is N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions is constituted as a topological graph, i.e. there is directed edges connecting some positions, and no CYCL E exists. II Players you and I move chesses alternately. In each turn the player should move is only a chess from the current position to one of its out-positions along an edge. The game does not end, until one of the players cannot move chess any more. If You cannot move any chess in your turn, you lose. Otherwise, if the misfortune falls on me ... I'll disturb the chesses and play it again.
Do you want to challenge me? Just Write your program to show your qualification!
Input
Input contains multiple test cases. Each test case is starts with a number n (1 <= n <=) on one line. Then the following N lines describe the out-positions of each position. Each line starts with a integer Xi is the number of out-positions for the position I. Then Xi integers following specify the out-positions. Positions is indexed from 0 to N-1. then multiple queries follow. Each query is occupies only one line. The line starts with a number M (1 <= m <=), and then come M integers, which is the initial positions of chesses . A line with number 0 ends the test case.
Output
There is one line for each query, which contains a string "WIN" or "Lose". "Win" means that the player taking the first turn can WIN the game according to a clever strategy; Otherwise "lose" should be printed.
Sample Input
42 1 201 301 02 0 2041 11 2002 0 12 1 13 0 1 30
Sample Output
Winwinwinlosewin
Hint
Huge input,scanf is recommended.
Source
PKU monthly,chen Shixi (Xreborner)
Ideas
sg function.
Consider each query as a set of games, ANS is the XOR value of the SG function for each group of games.
SG (x) =mex (s), where Mex (s) represents the smallest of the SG values that are not present in the successor set.
This problem has learned an interesting lesson: because the VIS each layer DFS will be used independently, so if placed outside Dfs declared as a global variable, then the VIS will be overwritten by the following DFS.
Code
1#include <cstdio>2#include <vector>3#include <cstring>4#include <iostream>5 #definefor (A,B,C) for (int a= (b);a< (c); a++)6 using namespacestd;7 8 Const intN = ++Ten;9 Ten intN,m,sg[n]; Onevector<int>G[n]; A - intDfsintu) { - if(sg[u]!=-1)returnSg[u]; the intVis[n]; -memset (Vis,0,sizeof(Vis)); -for (I,0, G[u].size ()) -Vis[dfs (G[u][i])]=1; + for(intI=0;; i++) - if(!vis[i])returnsg[u]=i; + } A intMain () { at while(SCANF ("%d", &n) = =1) { -for (I,0, N) G[i].clear (); - intv; -for (I,0, N) { -scanf"%d",&m); -For (J,0, M) { inscanf"%d",&v); - G[i].push_back (v); to } + } -memset (sg,-1,sizeof(SG)); the while(SCANF ("%d", &m) &&m) { * intans=0, q; $for (I,0, M)Panax Notoginsengscanf"%d", &q), ans^=DFS (q); - if(ANS) puts ("WIN");ElsePuts"lose"); the } + } A return 0; the}
POJ 2425 A Chess Game (SG function)