hdu 1524 A Chess Game 博弈

时间:2023-03-08 17:05:31

题目链接

给出一个有向无环图, 上面放有一些棋子, 两个人轮流移动棋子, 当一个人无法移动时, 算输。

求出每一个点的sg值, 异或就可以。出度为0的点sg值为0。

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e6+;
int head[maxn], sg[], num;
struct node
{
int to, nextt;
}e[maxn];
void init() {
mem1(head);
num = ;
mem1(sg);
}
void add(int u, int v) {
e[num].to = v;
e[num].nextt = head[u];
head[u] = num++;
}
int mex(int x) {
if(~sg[x])
return sg[x];
bool vis[];
memset(vis, false, sizeof(vis));
for(int i = head[x]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(sg[v]==-)
sg[v] = mex(v);
vis[sg[v]] = ;
}
for(int i = ; ; i++)
if(!vis[i])
return i;
}
int main()
{
int n, x, y;
while(scanf("%d", &n)!=EOF) {
init();
for(int i = ; i<n; i++) {
scanf("%d", &x);
while(x--) {
scanf("%d", &y);
add(i, y);
}
}
while(scanf("%d", &x)&&x) {
int ans = ;
for(int i = ; i<x; i++) {
scanf("%d", &y);
ans ^= mex(y);
}
if(ans) {
puts("WIN");
} else {
puts("LOSE");
}
}
}
}