[Original blog] poj 2425 a chess game

Source: Internet
Author: User

Question Link
Given a directed acyclic graph (DAG) with some flags on it, the flag can overlap. Two people can take turns and move a flag from one position to another, if the device cannot be moved, enter and ask if the player must win.

This question can regard each flag as a separate game. Then, the SG values of all these flags are the XOR and SG values of these flags, which can be memorized for searching DFS, the SG value can be used for brute force calculation.

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring> //by zrt//problem:using namespace std;typedef long long LL;const int inf(0x3f3f3f3f);const double eps(1e-9);int n;int H[1005],X[1000005],P[1000005],tot;inline void add(int x,int y){    P[++tot]=y;X[tot]=H[x];H[x]=tot;}int sg[1005];int dfs(int x){    if(~sg[x]) return sg[x];    bool nxt[1005];    memset(nxt,0,sizeof nxt);    for(int i=H[x];i;i=X[i]){        nxt[dfs(P[i])]=1;    }    for(int i=0;;i++){        if(!nxt[i]){            return sg[x]=i;        }    }}int main(){    #ifdef LOCAL    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    #endif    while(~scanf("%d",&n)){        memset(H,0,sizeof H);tot=0;        memset(sg,-1,sizeof sg);        for(int i=0,x;i<n;i++){            scanf("%d",&x);            for(int j=0,y;j<x;j++){                scanf("%d",&y);                add(i,y);            }        }        int q;        while(scanf("%d",&q),q){            int SG=0;            for(int i=0,x;i<q;i++){                scanf("%d",&x);                SG^=dfs(x);            }            if(SG) puts("WIN");            else puts("LOSE");        }    }    return 0;}
View code

 

[Original blog] poj 2425 a chess game

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.