POJ 2425 A Chess Game(有向图SG函数)题解
题意:给一个有向图,然后个m颗石头放在图上的几个点上,每次只能移动一步,如果不能移动者败
思路:dfs打表sg函数,然后求异或和
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define eps 1e-9
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
vector<int> G[maxn];
int in[maxn], n;
int s[maxn], sg[maxn];
void dfs(int u){
if(G[u].size() == ){
sg[u] = ;
return;
}
for(int i = ; i < G[u].size(); i++){
int v = G[u][i];
if(sg[v] == -)
dfs(v);
}
memset(s, , sizeof(s));
for(int i = ; i < G[u].size(); i++){
int v = G[u][i];
s[sg[v]] = ;
}
for(int i = ; i < maxn; i++){
if(!s[i]){
sg[u] = i;
return;
}
}
}
int main(){
while(~scanf("%d", &n)){
memset(in, , sizeof(in));
memset(sg, -, sizeof(sg));
for(int i = ; i <= n - ; i++){
int x, to;
G[i].clear();
scanf("%d", &x);
while(x--){
scanf("%d", &to);
G[i].push_back(to);
in[to]++;
}
}
for(int i = ; i <= n - ; i++){
if(!in[i]){
dfs(i);
}
}
int m;
while(scanf("%d", &m) && m){
int ans = , x;
while(m--){
scanf("%d", &x);
ans ^= sg[x];
}
if(ans == ) printf("LOSE\n");
else printf("WIN\n");
}
}
return ;
}
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
http://poj.org/problem?id=2425 #include<iostream> #include<cstdio> #include<cstring&g ...
- POJ2425 A Chess Game[博弈论 SG函数]
A Chess Game Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3917 Accepted: 1596 Desc ...
- poj 2425 A Chess Game 博弈论
思路:SG函数应用!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include< ...
- poj 3575 Crosses and Crosses(SG函数)
Crosses and Crosses Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3063 Accepted: 11 ...
- poj 2425 A Chess Game_sg函数
题意:给你一个有向无环图,再给你图上的棋子,每人每次只能移动一个棋子,当轮到你不能移动棋子是就输了,棋子可以同时在一个点 比赛时就差这题没ak,做了几天博弈终于搞懂了. #include <io ...
- pku 2425 A Chess Game (SG)
题意: 给一个由N个点组成的一张有向图,不存在环.点的编号是0~N-1. 然后给出M个棋子所在的位置(点的编号)[一个点上可同时有多个棋子]. 每人每次可移动M个棋子中的一个棋子一步,移动方向是有向边 ...
- HDU 1524 A Chess Game【SG函数】
题意:一个N个点的拓扑图,有M个棋子,两个人轮流操作,每次操作可以把一个点的棋子移动到它的一个后继点上(每个点可以放多个棋子),直到不能操作,问先手是否赢. 思路:DFS求每个点的SG值,没有后继的点 ...
随机推荐
- 【python-opencv】18-图像梯度+图像边界
效果图: *一阶导数与Soble算子 *二阶导数与拉普拉斯算子 定义:把图片想象成连续函数,因为边缘部分的像素值是与旁边像素明显有区别的,所以对图片局部求极值,就可以得到整幅图片的边缘信息了. 不过图 ...
- javaScript 载入自执行
1.注册可以直接调用f()中的b(),c(),d() .原因?自己想. <!DOCTYPE html> <html> <head> <meta charset ...
- 【JMeter】如何优雅的写脚本
cc给发的视频链接: http://v.youku.com/v_show/id_XMzA4Mjg1ODA0MA==.html?spm=a2h3j.8428770.3416059.1 ————————— ...
- PHP主动断开与浏览器的连接
以前整理过一篇<关于PHP连接处理中set_time_limit().connection_status()和ignore_user_abort()深入解析>,是解说浏览器client断开 ...
- CSS分列等高
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Java-mybatis-一次执行多条SQL语句
mysql数据库 1.修改数据库连接参数加上allowMultiQueries=true,如: hikariConfig.security.jdbcUrl=jdbc:mysql://xx.xx.xx: ...
- golang语言调试
https://software.intel.com/en-us/blogs/2014/05/10/debugging-performance-issues-in-go-programs https: ...
- postman 安装,对elasticsearch进行请求
1 使用postman对elasticsearch进行测试 :下载插件: https://www.getpostman.com/apps ,下载时exe文件,双击自动安装,首次打开注册.下面就可以使 ...
- R中基本函数学习[转载]
转自:https://www.douban.com/note/511740050/ 1.数据管理 numeric:数值型向量 logical:逻辑型向量 character:字符型向量list:列表 ...
- 使用tagName定位报错
使用标签进行定位元素,页面报错,由于input标签不唯一,webdriver默认会取第一个元素,但是第一个input元素的类型是‘hidden’,无法展示,因此程序就报错了 如何解决,未完待续...