poj 2425 A Chess Game_sg函数
题意:给你一个有向无环图,再给你图上的棋子,每人每次只能移动一个棋子,当轮到你不能移动棋子是就输了,棋子可以同时在一个点
比赛时就差这题没ak,做了几天博弈终于搞懂了.
#include <iostream>
#include <cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define N 1010
vector<int> adj[N];
int sg[N],n;
int mex(int v){
bool vis[N]={0};
int i,w;
for(i=0;i<adj[v].size();i++){
w=adj[v][i];
if(sg[w]==-1)
sg[w]=mex(w);
vis[sg[w]]=1;
}
for(i=0;;i++)
if(vis[i]==0)
return i;
}
int main(int argc, char** argv) {
int m,v,w,sum;
while(scanf("%d",&n)!=EOF){
for(v=0;v<n;v++){
adj[v].clear();
scanf("%d",&m);
while(m--){
scanf("%d",&w);
adj[v].push_back(w);
}
}
memset(sg,-1,sizeof(sg));
while(scanf("%d",&m)!=EOF&&m){
sum=0;
while(m--){
scanf("%d",&v);
if(sg[v]==-1)
sg[v]=mex(v);
sum^=sg[v];
}
if(sum)
printf("WIN\n");
else
printf("LOSE\n");
}
}
return 0;
}
poj 2425 A Chess Game_sg函数的更多相关文章
- POJ 2425 A Chess Game 博弈论 sg函数
http://poj.org/problem?id=2425 典型的sg函数,建图搜sg函数预处理之后直接求每次游戏的异或和.仍然是因为看不懂题目卡了好久. 这道题大概有两个坑, 1.是搜索的时候vi ...
- poj 2425 A Chess Game(SG函数)
A Chess Game Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3551 Accepted: 1440 Desc ...
- POJ 2425 A Chess Game(有向图SG函数)题解
题意:给一个有向图,然后个m颗石头放在图上的几个点上,每次只能移动一步,如果不能移动者败 思路:dfs打表sg函数,然后求异或和 代码: #include<queue> #include& ...
- POJ 2425 A Chess Game#树形SG
http://poj.org/problem?id=2425 #include<iostream> #include<cstdio> #include<cstring&g ...
- poj 2425 A Chess Game 博弈论
思路:SG函数应用!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include< ...
- [原博客] POJ 2425 A Chess Game
题目链接题意:给定一个有向无环图(DAG),上面放有一些旗子,旗子可以重合,两个人轮流操作,每次可以把一个旗子从一个位置移动到相邻的位置,无法移动时输,询问先手是否必胜. 这道题可以把每个旗子看作单独 ...
- poj 2425 AChessGame(博弈)
A Chess Game Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3791 Accepted: 1549 Desc ...
- A Chess Game POJ - 2425
Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesse ...
- POJ 2480 (约数+欧拉函数)
题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n ...
随机推荐
- MSSQL WITH (NOLOCK) 脏读
缺点: 1.会产生脏读 2.只适用与select查询语句 优点: 1.有些文件说,加了WITH (NOLOCK)的SQL查询效率可以增加33%. 2.可以用于inner join 语句 脏读: 一个用 ...
- Pascal's Triangle 解答
Question Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- poj 1088 动态规划+dfs(记忆化搜索)
滑雪 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Description Mi ...
- (转)Java 的swing.GroupLayout布局管理器的使用方法和实例
摘自http://www.cnblogs.com/lionden/archive/2012/12/11/grouplayout.html (转)Java 的swing.GroupLayout布局管理器 ...
- django 启动和请求
Django运行方式 调试模式 直接 python manage.py runserver python manage.py runserver python manage.py runserver ...
- Android应用程序中的多个Activity的显示创建和调用
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTkzNjE0Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Sublime Text 添加到右键菜单 带菜单图标
1.打开 regedit 2.找到节点 HKEY_CLASSSES_ROOT -> * -> Shell 3.右键选择新建“ 项 ” 这个项的名字将作为右键菜单的菜单名称,我用的“ Sub ...
- SQL Server 两种判断表名是否存在且删除的方式
邓老师(老邓)教的 if exists(select * from sysobjects where name='Table_88') drop table Table_88 偷的((*^__^ ...
- 记录ASP.NET页面表单初始状态(主要是为了前台可以根据这个判断页面是否变动了)
把页面表单状态记录到HiddenField中. 这里只提供后台代码, 前台逻辑根据需求自由定义. 存放值的ViewState: protected Dictionary<string, stri ...
- 76 bytes for faster jQuery
转载自http://james.padolsey.com/javascript/76-bytes-for-faster-jquery/ 作者JAMES PADOLSEY 在我们平时使用JQuery,调 ...