HDU_1524 A Chess Game (sg函数)

  /*今天一翻以前做的sg函数的题,这题原来的代码居然跑781MS, 我晕!又重新写
了一遍,开始vis数组定义成全局变量了,WA。。。T_T

My Code(78+MS):
*/

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1005;

struct node{
int num;
int edg[N];
}e[N];

int sg[N];


int mex(int t){
if(sg[t] != -1) return sg[t];
int i;
bool vis[N] = {0};

for(i = 0; i < e[t].num; i++){
vis[mex(e[t].edg[i])] = 1;
}
for(i = 0; ; i++){
if(!vis[i]){
sg[t] = i;
break;
}
}
return sg[t];
}

int main(){
//freopen("data.in", "r", stdin);

int n, i, j, m, flag, k;
while(~scanf("%d", &n)){
memset(e, 0, sizeof(e));
for(i = 0; i < n; i++){
scanf("%d", &e[i].num);
for(j = 0; j < e[i].num; j++){
scanf("%d", &e[i].edg[j]);
}
}
for(i = 0; i < N; i++){
sg[i] = -1;
}
for(i = 0; i < n; i++){
mex(i);
}
while(scanf("%d", &m), m){
flag = 0;
while(m--){
scanf("%d", &k);
flag ^= sg[k];
}
if(flag) puts("WIN");
else puts("LOSE");
}
}
return 0;
}



你可能感兴趣的:(game)