【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 ...
随机推荐
- Mac上pod install一直停住的解决办法
pod install一直停住的解决办法 在/Users/XXX/.cocoapods/repos下 git clone https://github.com/CocoaPods/Specs.git ...
- NDK开发之调用方法
与NDK开发之访问域中介绍的一样,Java中的方法也是分为两类:实例方法和静态方法.JNI提供了访问两类方法的函数,下面我们一起来看看怎么在C中访问Java中的方法. 我们的MainActivity中 ...
- Effective C++ 笔记二 构造/析构/赋值运算
条款05:了解C++默默编写并调用哪些函数 编译器默认声明一个default构造函数.一个copy构造函数.一个copy assignment操作符和一个析构函数.这些函数都是public且inlin ...
- C#面向对象(一)
一:面向对象的基本知识 C#程序分为面向过程和面向对象 什么是对象:一切皆为对象:Object,生活中常说的“东西”就是程序里面所指的对象:生活中遇到的东西我们都在下意识的归类:归类意味着抽象模型: ...
- C# 序列化和反序列
1.对象序列化的介绍 (1).NET支持对象序列化的几种方式 二进制序列化:对象序列化之后是二进制形式的,通过BinaryFormatter类来实现的,这个类位于System.Runtime.Seri ...
- PHP+jQuery+Ajax实现用户登录与退…
用户登录与退出功能应用在很多地方,而在有些项目中,我们需要使用Ajax方式进行登录,登录成功后只刷新页面局部,从而提升了用户体验度.本文将使用PHP和jQuery来实现登录和退出功能. 查看演示DEM ...
- Asp.net Mvc对比Php的4大误解
一:asp.net技术已过时,Php技术更新 Asp.net mvc 5 发布于2014 夏天. 二:php开发者更多,所以更能得到帮助 2者对比犹如下图,会拿电锯的肯定多少会点锯子, 会用锯子的不一 ...
- C#基础加强
1.代码规范 -命名规范:定义的变量或者方法名要有意义. 1.骆驼命名 :第一个单词首字母小写,之后的单词首字母大写 userName.userPassword.realName…(普通变量(局部变量 ...
- CoreAnimation4-隐式动画和显式动画
事务 Core Animation基于一个假设,说屏幕上的任何东西都可以(或者可能)做动画.动画并不需要你在Core Animation中手动打开,相反需要明确地关闭,否则他会一直存在. 当你改变CA ...
- NSAttributedString用法
以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILabel,“元/位”一个UILabel.今天翻看以前的工程,command点进UITextField中看到[attrib ...