【POJ3710】Christmas Game (博弈-树上的删边问题)
【题目】
Description Harry and Sally were playing games at Christmas Eve. They drew some Christmas trees on a paper: Then they took turns to cut a branch of a tree, and removed the part of the tree which had already not connected with the root. A step shows as follows: Sally always moved first. Who removed the last part of the trees would win the game. After a while, they all figured out the best strategy and thought the game was too simple for them. Harry said, “The Christmas trees should have some gifts in them!” So Sally drew some gifts (simple polygons) in the initial trees: You may assume the initial picture is a tree with some simple polygons, in which each edge is involved in at most one polygon. We also know that every polygon has only one node involved in the main tree (the hanging point of the giftJ) .In every sub-tree (connected subgraph), there was one and only one node representing the “root”. According to these assumptions, following graphs will never appear: Sally and Harry took turns (Sally was always the first person to move), to cut an edge in the graph, and removed the part of the tree that no longer connected to the root. The person who cannot make a move lost the game. Your job is to decide who will finally win the game if both of them use the best strategy. Input The input file contains multiply test cases. Output For each test case, output the name of the winner. Sample Input 2 Sample Output Sally |
【题意】
【分析】
这道题在贾志豪的论文《组合游戏略述——浅谈SG游戏的若干拓展及变形》中有。
于是做这题的时候我花了很长时间苦思冥想、然后花了很长时间看题解看不懂,然后花了很长时间理解整个过程。表示脑子不够用啊。其实一开始那一步,就是变成很多棵根节点只有一个孩子的树的子游戏,把他们异或起来,这一步我是想到的。但是对于子游戏的sg值的求法和环的处理我不是很明白。先说环,偶环是无用的,因为别人做什么我就做什么。对于奇环,我们给他留一个点就好了,同样是别人做什么我就做什么。其实这个思路很常规了,在扫楼梯等题目中已经多次用到,但是我还是没想到这个层面(或许是因为一开始看错题了,不知道环只会在尾部吧)。最难的就是对于一棵树(含边u->v以及以v为根的子树组成的树),它的sg等于v这棵树的sg+1。这个的证明用到了数学归纳法(貌似,就是先说明在数据较小的情况下是成立的,以小推大再推到全部),具体看论文吧,就这个点比较难,需要好好理解。
算出每棵树的sg后,再按NIM游戏异或即可。
代码如下:(巨巨巨巨丑)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define Maxn 110
#define Maxm 110
#define Maxk 550 struct node
{
int x,y,next,p;
}t[Maxk*];int len;
int first[Maxn*],fa[Maxn*],dep[Maxn*];
int n,m; bool vis[Maxn],mark[Maxn]; void ins(int x,int y)
{
t[++len].x=x;t[len].y=y;t[len].p=;
t[len].next=first[x];first[x]=len;
} stack<int > s; int ffind(int x,int f)
{
vis[x]=;
dep[x]=dep[f]+;
//s.push(x);
int now=;
for(int i=first[x];i;i=t[i].next) if(t[i].p==)
{
int y=t[i].y;
t[i].p=;t[(i%==)?i-:i+].p=;
if(vis[y])
{
t[i].p=-,t[(i%==)?i-:i+].p=-;
if((dep[x]-dep[y]+)%==) ins(y,++m);
return y;
}
else if(now=ffind(y,x)) t[i].p=-,t[(i%==)?i-:i+].p=-;
}
if(x==now) return ;
return now;
} int get_sg(int x,int f)
{
int ans=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f&&t[i].p!=-)
{
ans^=(get_sg(t[i].y,x)+);
}
return ans;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int ans=;
while(n--)
{
int k;
scanf("%d%d",&m,&k);
memset(first,,sizeof(first));
len=;
for(int i=;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ins(x,y);ins(y,x);
}
memset(vis,,sizeof(vis));
memset(mark,,sizeof(mark));
dep[]=;
ffind(,);
ans^=get_sg(,);
}
if(ans==) printf("Harry\n");
else printf("Sally\n");
}
return ;
}
[POJ3710]
2016-04-21 12:58:40
【POJ3710】Christmas Game (博弈-树上的删边问题)的更多相关文章
- poj 3710 Christmas Game(树上的删边游戏)
Christmas Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1967 Accepted: 613 Des ...
- POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)
题目链接 \(Description\) 给定n棵"树",每棵"树"的节点可能"挂着"一个环,保证没有环相交,且与树只有一个公共点. 两人轮 ...
- 【BZOJ 2688】 2688: Green Hackenbush (概率DP+博弈-树上删边)
2688: Green Hackenbush Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 42 Solved: 16 Description ...
- POJ3710 Christmas Game 博弈论 sg函数 树的删边游戏
http://poj.org/problem?id=3710 叶子节点的 SG 值为0:中间节点的SG值为它的所有子节点的SG值加1后的异或和. 偶环可以视作一个点,奇环视为一条边(连了两个点). 这 ...
- POJ 3710 Christmas Game [博弈]
题意:略. 思路:这是个删边的博弈游戏. 关于删边游戏的预备知识:http://blog.csdn.net/acm_cxlove/article/details/7854532 学习完预备知识后,这一 ...
- poj3710 Christmas Game
题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...
- hihocoder1545 : 小Hi和小Ho的对弈游戏(树上博弈&nim博弈)
描述 小Hi和小Ho经常一起结对编程,他们通过各种对弈游戏决定谁担任Driver谁担任Observer. 今天他们的对弈是在一棵有根树 T 上进行的.小Hi和小Ho轮流进行删除操作,其中小Hi先手. ...
- 【Mark】博弈类题目小结(HDU,POJ,ZOJ)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 首先当然要献上一些非常好的学习资料: 基础博弈的小 ...
- 博弈论BOSS
基础博弈的小结:http://blog.csdn.net/acm_cxlove/article/details/7854530 经典翻硬币游戏小结:http://blog.csdn.net/acm_c ...
随机推荐
- 使用tuple返回多个值
17.4编写并测试findbook函数 #include<iostream> #include<vector> #include<string> #include& ...
- 【Android】isEmpty()
对字符串进行非空判断,可以一次性进行两种空值的判断.当传入的字符串等于null或者等于空字符串的时候这个方法都会返回true,从而不需要单独去判断这两种空值,再利用逻辑运算符连接起来.
- Windows Server 2008 R2中关闭“IE增强的安全配置”
当在Windows Sever 2008 R2中运动IE8的时候会发现默认情况下IE启用了增强的安全配置,为了方便而且是在内网的情况下我们可以关闭IE8的增强安全配置,操作很简单如下步骤. 一,以本机 ...
- centos7安装chrome的历程(fedora同)
安装 首先是下载,地址奉上:http://www.google.cn/chrome/browser/desktop/index.html,选择64 bit .rpm (适用于 Fedora/openS ...
- 安装SQL Server2005出现 IIS警告原因
出现此问题的原因取决于 SQL Server 的不是所有 IIS 7.0 组件都安装在计算机上.下表列出了受影响的组件. 组件 文件夹 静态内容 常见的 HTTP 功能 默认文档 常见的 HTTP 功 ...
- Js完美验证15/18身份证,Js验证身份证,支持15/18位
Js完美验证15/18身份证,Js验证身份证,支持15/18位 >>>>>>>>>>>>>>>>> ...
- 当append里面的标签显示不出来的时候,用下面的方式做
$("#result_td").append(tem1+tem3) $("#result_td").append($(tem1+tem3))
- html不同文档类型支持的元素标签
- CenOs安装中文输入法
http://jingyan.baidu.com/album/d8072ac4434666ec95cefda1.html?picindex=2 查看链接
- (转)Spring读书笔记-----Spring核心机制:依赖注入
Java应用(从applets的小范围到全套n层服务端企业应用)是一种典型的依赖型应用,它就是由一些互相适当地协作的对象构成的.因此,我们说这些对象间存在依赖关系.加入A组件调用了B组件的方法,我们就 ...