Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
AC代码+讲解#include<cstdio> #include<vector> #include<cstring> using namespace std; #define ll long long #define ri register #define pii pair<int,int> const int inf=0x3f3f3f3f; const int N=1e6+5; int n,m,u,sg[N]; vector<int>e[N]; bool vis[N],bar[N]; int SG(int x) { if(vis[x])return sg[x]; vis[x]=1; int v; for(int i=0;i<e[x].size();i++) v=e[x][i],SG(v);//用vector存图,因为没有具体说明边的数目 memset(bar,0,sizeof bar); for(int i=0;i<e[x].size();i++) v=e[x][i],bar[SG(v)]=1; if(!e[x].size())return sg[x]=0; for(int i=0;;i++) if(!bar[i])return sg[x]=i;//这部分相当于取mex } signed main() { while(~scanf("%d",&n)) { memset(vis,0,sizeof vis); memset(sg,0,sizeof sg); for(int i=1;i<=n;i++) { scanf("%d",&m); while(m--)scanf("%d",&u),e[i].push_back(u+1); } while(1) { scanf("%d",&m); if(!m)break; int ans=0; while(m--)scanf("%d",&u),ans^=SG(u+1); if(!ans)printf("LOSE\n"); else printf("WIN\n"); } for(int i=1;i<=n;i++)e[i].clear(); } return 0; } 本题其实是SG函数的模板题目 对于SG函数的证明可以借鉴于Nim游戏的证法,具体的可以看OIwiki Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator