Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions are constituted as a topological graph, i.e. there are directed edges connecting some positions, and no cycle exists. Two players you and I move chesses alternately. In each turn the player should move only one chess from the current position to one of its out-positions along an edge. The game does not end, until one of the players cannot move chess any more. If you cannot move any chess in your turn, you lose. Otherwise, if the misfortune falls on me... I will disturb the chesses and play it again.

Do you want to challenge me? Just write your program to show your qualification!

Input

Input contains multiple test cases. Each test case starts with a number N (1 <= N <= 1000) in one line. Then the following N lines describe the out-positions of each position. Each line starts with an integer Xi that is the number of out-positions for the position i. Then Xi integers following specify the out-positions. Positions are indexed from 0 to N-1. Then multiple queries follow. Each query occupies only one line. The line starts with a number M (1 <= M <= 10), and then come M integers, which are the initial positions of chesses. A line with number 0 ends the test case.

Output

There is one line for each query, which contains a string "WIN" or "LOSE". "WIN" means that the player taking the first turn can win the game according to a clever strategy; otherwise "LOSE" should be printed.

Sample Input

4
2 1 2
0
1 3
0
1 0
2 0 2
0 4
1 1
1 2
0
0
2 0 1
2 1 1
3 0 1 3
0

Sample Output

WIN
WIN
WIN
LOSE
WIN

思路:树上nim,叶子节点nim为0,父亲节点递归儿子得到sg值,答案就是每个石子所在点的sg值异或和。

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int mp[][];
int SG[];
int N;
int DFS(int n)
{
int i;
if(SG[n]!=-)
return SG[n];
bool next[];
memset(next, , sizeof(next));
for(i = ; i < N; ++i)
{
if(mp[n][i] != -)
next[DFS(i)] = ;
}
i = ;
while(next[i])
i++;
return SG[n] = i;
}
int main()
{
int i, j, k, t;
int X;
int tp, ans;
while(scanf("%d", &N)!=EOF)
{
memset(mp, -, sizeof(mp));
memset(SG, -, sizeof(SG));
for(i = ; i < N; ++i)
{
scanf("%d", &k);
if(k==) SG[i] = ;
for(j = ; j < k; ++j)
{
scanf("%d", &t);
mp[i][t] = ;
}
}
while(scanf("%d", &X))
{
if(X==) break;
ans = ;
for(i = ; i < X; ++i)
{
scanf("%d", &tp);
ans = ans ^ DFS(tp);
}
if(ans) printf("WIN\n");
else printf("LOSE\n");
}
}
return ;
}

转载

A Chess Game POJ - 2425的更多相关文章

  1. poj 2425 AChessGame(博弈)

    A Chess Game Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 3791   Accepted: 1549 Desc ...

  2. POJ 2425 A Chess Game#树形SG

    http://poj.org/problem?id=2425 #include<iostream> #include<cstdio> #include<cstring&g ...

  3. POJ 2425 A Chess Game 博弈论 sg函数

    http://poj.org/problem?id=2425 典型的sg函数,建图搜sg函数预处理之后直接求每次游戏的异或和.仍然是因为看不懂题目卡了好久. 这道题大概有两个坑, 1.是搜索的时候vi ...

  4. poj 2425 A Chess Game(SG函数)

    A Chess Game Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 3551   Accepted: 1440 Desc ...

  5. poj 2425 A Chess Game 博弈论

    思路:SG函数应用!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include< ...

  6. [原博客] POJ 2425 A Chess Game

    题目链接题意:给定一个有向无环图(DAG),上面放有一些旗子,旗子可以重合,两个人轮流操作,每次可以把一个旗子从一个位置移动到相邻的位置,无法移动时输,询问先手是否必胜. 这道题可以把每个旗子看作单独 ...

  7. poj 2425 A Chess Game_sg函数

    题意:给你一个有向无环图,再给你图上的棋子,每人每次只能移动一个棋子,当轮到你不能移动棋子是就输了,棋子可以同时在一个点 比赛时就差这题没ak,做了几天博弈终于搞懂了. #include <io ...

  8. POJ 2425 A Chess Game(有向图SG函数)题解

    题意:给一个有向图,然后个m颗石头放在图上的几个点上,每次只能移动一步,如果不能移动者败 思路:dfs打表sg函数,然后求异或和 代码: #include<queue> #include& ...

  9. SG函数和SG定理【详解】

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

随机推荐

  1. Gym 101775J Straight Master(差分数组)题解

    题意:给你n个高度,再给你1~n每种高度的数量,已知高度连续的3~5个能消去,问你所给的情况能否全部消去:例:n = 4,给出序列1 2 2 1表示高度1的1个,高度2的2个,高度3的2个,高度4的1 ...

  2. .net Core 2.1 后 Session保存,新页面获取不到值

    https://blog.csdn.net/kuui_chiu/article/details/81060051 https://blog.csdn.net/niunan/article/detail ...

  3. (转) Read-through: Wasserstein GAN

    Sorta Insightful Reviews Projects Archive Research About  In a world where everyone has opinions, on ...

  4. ExceptionLogger

    应用1:webconfig.cs中设置 public static class WebApiConfig { public static void Register(HttpConfiguration ...

  5. 【转载】vim 中如何替换选中行或指定几行内的文本

    https://segmentfault.com/q/1010000002552573/a-1020000002552589 :'<,'>s/替换项/替换为/g 以下命令将文中所有的字符串 ...

  6. DIV+CSS+PS实现背景图的三层嵌套以及背景图的合并

    传说中的“三层嵌套技术”. 一.背景图合并: div+css+ps合图相结合的技术:通过精确到1px的css设置,使用ps合成背景图片,特别是小图片合并,来完成页面效果.         首先讲讲三层 ...

  7. Aviutl 视频处理软件

    素材类:No.009 倒放(Video)         http://www.bilibili.com/video/av3078207/ No.010 倒放(Object)         http ...

  8. 2、My Scripts

    http://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html      注释详解 1.打印选择菜单,按照选择项一键安装不同的web服 ...

  9. axios的学习与使用

    最近的项目都是使用的vue框架,所以请求都使用了vue官方推荐的axios. 官方中文介绍 此处记录一下常用的写法 执行 GET 请求 // 为给定 ID 的 user 创建请求 axios.get( ...

  10. 踩坑记录:spring boot的POST请求数据注入不了的问题

    概述: 今天在使用spring boot框架的时候,踩了一个坑,是关于control层request body依赖注入的问题的,内容如下: 进过: 由于目前公司采用的系统架构,要求把springboo ...