POJ 2425 A Chess Game 博弈论 sg函数
http://poj.org/problem?id=2425
典型的sg函数,建图搜sg函数预处理之后直接求每次游戏的异或和。仍然是因为看不懂题目卡了好久。
这道题大概有两个坑,
1.是搜索的时候vis数组应该在函数内声明(似乎这是我经常在搜索里犯的错误,为了省一点空间整道题都写错了);
2.是n个点的有向无环图边数上限是n^2(re了好久QAQ)。
在漫长的查资料过程之后终于大概搞懂了sg函数的原理,愉快。下一篇大概会写一个小结。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<map>
using namespace std;
const int maxn=;
int n,m;
struct nod{
int y,next;
}e[maxn*maxn];
int head[maxn]={},tot=;
int f[maxn]={};
void dfs(int x){
if(f[x]!=-)return;
if(head[x]==){
f[x]=;return;
}
bool vis[maxn]={};
for(int i=head[x];i;i=e[i].next){
dfs(e[i].y);
vis[f[e[i].y]]=;
}
for(int i=;i<=n;i++){
if(!vis[i]){
f[x]=i;break;
}
}
}
int main(){
while(~scanf("%d",&n)){
int x,y;
memset(head,,sizeof(head));
memset(f,-,sizeof(f));
tot=;
for(int i=;i<=n;i++){
scanf("%d",&x);
for(int j=;j<=x;j++){
scanf("%d",&y);
e[++tot].y=y+;
e[tot].next=head[i];
head[i]=tot;
}
}
for(int i=;i<=n;i++){
if(f[i]==-)dfs(i);
}
while(~scanf("%d",&m)){
if(m==)break;
y=;
for(int i=;i<=m;i++){
scanf("%d",&x);
y^=f[x+];
}
if(y){
printf("WIN\n");
}
else{
printf("LOSE\n");
}
}
}
return ;
}
POJ 2425 A Chess Game 博弈论 sg函数的更多相关文章
- poj 2425 A Chess Game(SG函数)
A Chess Game Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3551 Accepted: 1440 Desc ...
- 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 2425 A Chess Game#树形SG
http://poj.org/problem?id=2425 #include<iostream> #include<cstdio> #include<cstring&g ...
- POJ 2425 A Chess Game(有向图SG函数)题解
题意:给一个有向图,然后个m颗石头放在图上的几个点上,每次只能移动一步,如果不能移动者败 思路:dfs打表sg函数,然后求异或和 代码: #include<queue> #include& ...
- POJ 2960 S-Nim 博弈论 sg函数
http://poj.org/problem?id=2960 sg函数几乎是模板题. 调试代码的最大障碍仍然是手残在循环里打错变量名,是时候换个hydra产的机械臂了[超想要.jpg] #includ ...
- 2016多校联合训练1 B题Chess (博弈论 SG函数)
题目大意:一个n(n<=1000)行,20列的棋盘上有一些棋子,两个人下棋,每回合可以把任意一个棋子向右移动到这一行的离这个棋子最近的空格上(注意这里不一定是移动最后一个棋子),不能移动到棋盘外 ...
- 【基础操作】博弈论 / SG 函数详解
博弈死我了……(话说哪个小学生会玩博弈论提到的这类弱智游戏,还取石子) 先推荐两个文章链接:浅谈算法——博弈论(从零开始的博弈论) 博弈论相关知识及其应用 This article was updat ...
- bzoj1188 [HNOI2007]分裂游戏 博弈论 sg函数的应用
1188: [HNOI2007]分裂游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 973 Solved: 599[Submit][Status ...
随机推荐
- Angular2.0 基础: User Input
1.Angular 2.0 中的变量 对输入值的获取,我们可以通过$event 来获取,也可以通过变量来获取. template: ` <input (keyup)="onKey($e ...
- Java多线程学习(四)等待/通知(wait/notify)机制
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
- exit() _exit() 函数区别
exit(): --stdlib.h (1) 所有使用atexit()注册的函数,将会被以注册相反的顺序调用: (2) 所有打开的输出流被刷新,并且关闭流: (3) 使用tmpfile()创建的文件将 ...
- Python基础=== Tkinter Grid布局管理器详解
本文转自:https://www.cnblogs.com/ruo-li-suo-yi/p/7425307.html @ 箬笠蓑衣 Grid(网格)布局管理器会将控件放置到一个二维的表 ...
- freeradius防止用户异常断开无法重新链接上
freeradius防止用户异常断开无法重新链接上 http://www.cnblogs.com/klobohyz/archive/2012/02/08/2342532.html 编辑default文 ...
- 【bzoj1024】生日蛋糕
一开始想dp 后来发现既然要均等,又看了下数据条件 暴力就能做. #include<bits/stdc++.h> using namespace std; ; int x,y,n; dou ...
- [How to]如何自定义plist文件和读取plist文件内容
1.简介 plist作为IOS的固化文件,就好比java中properties文件,但是在IOS中plist是可读写的. 本文将介绍自定义静态的plist文件. 2.自定义静态plist文件 右击你的 ...
- C# 笔记——覆盖和重写
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- linux命令(48):nl命令
nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...
- Rotate Image——数学相关
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...