HDU ACM 1524 A Chess game-> game (SG function)

Source: Internet
Author: User

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)

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.