Test instructions: There are n vertices on a direction-free graph, each vertex can put a pawn or not put, there are two people, each time according to this chart can only move any one piece of a step, if to a certain step player can not move, then this person loses.
Analysis:
1, the game to the non-circular graph, DFS all the vertex of the SG values are calculated, and then each piece of the SG value of the different or operation, for 0 is the initiator will be defeated, or the initiator win.
2, if a person moves, all the pieces are in the out of the 0 vertex, then he will be defeated.
Introduction to the SG function:
A, for a given forward loop-free graph, define the Sprague-grundy function g for each vertex in the graph as follows: g (x) = mex{g (y) | Y is the successor of X.
The MEX (x) represents the smallest non-negative integer that does not belong to this collection. For example: mex{0,1,2,4} = 3, mex{2,3,5} = 0, mex{} = 0.
B, the properties of the SG function: first, the vertex corresponding to all endpoints, that is, the vertex with a degree of 0, its SG value is 0, because its successor is an empty set. Then for a
A g (x) = 0 vertex x, and all subsequent y of it satisfies the G (Y)!=0. For a vertex of a G (x)!=0, there must be a successor Y that satisfies the G (Y) =0.
C, the process of seeking the value of the whole SG function is a deep searching process for a direction-free graph.
#include <iostream> #include <vector>using namespace Std;int sg[1010];vector<int> map[1010];int GETSG (int x) {int i;if (SG[X]!=-1) return sg[x];if (Map[x].size () ==0) return Sg[x]=0;bool Vis[1010];memset (vis,0,sizeof ( VIS)); for (I=0;i<map[x].size (); i++) VIS[GETSG (Map[x][i])]=1;for (i=0;; i++) if (vis[i]==0) return sg[x]=i;} int main () {int n,m,i,xi,k;int res;while (cin>>n) {memset (sg,-1,sizeof (SG)); for (i=0;i<n;i++) {map[i].clear () ; Cin>>k;while (k--) {cin>>xi;map[i].push_back (Xi);}} while (cin>>m && M) {res=0;for (i=0;i<m;i++) {CIN>>XI;RES^=GETSG (Xi);} if (!res) puts ("Lose"); Else puts ("WIN");}} return 0;}
HDU ACM 1524 A Chess game-> game (SG function)