【题目】

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.
The first line of each test case is an integer N (N<100), which represents the number of sub-trees. The following lines show the structure of the trees. The first line of the description of a tree is the number of the nodes m (m<100) and the number of the edges k (k<500). The nodes of a tree are numbered from 1 to m. Each of following lines contains 2 integers a and b representing an edge <ab>. Node 1 is always the root.

Output

For each test case, output the name of the winner.

Sample Input

2
2 1
1 2
4 4
1 2
2 3
2 4
3 4

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 (博弈-树上的删边问题)的更多相关文章

  1. poj 3710 Christmas Game(树上的删边游戏)

    Christmas Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1967   Accepted: 613 Des ...

  2. POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)

    题目链接 \(Description\) 给定n棵"树",每棵"树"的节点可能"挂着"一个环,保证没有环相交,且与树只有一个公共点. 两人轮 ...

  3. 【BZOJ 2688】 2688: Green Hackenbush (概率DP+博弈-树上删边)

    2688: Green Hackenbush Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 42  Solved: 16 Description   ...

  4. POJ3710 Christmas Game 博弈论 sg函数 树的删边游戏

    http://poj.org/problem?id=3710 叶子节点的 SG 值为0:中间节点的SG值为它的所有子节点的SG值加1后的异或和. 偶环可以视作一个点,奇环视为一条边(连了两个点). 这 ...

  5. POJ 3710 Christmas Game [博弈]

    题意:略. 思路:这是个删边的博弈游戏. 关于删边游戏的预备知识:http://blog.csdn.net/acm_cxlove/article/details/7854532 学习完预备知识后,这一 ...

  6. poj3710 Christmas Game

    题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...

  7. hihocoder1545 : 小Hi和小Ho的对弈游戏(树上博弈&nim博弈)

    描述 小Hi和小Ho经常一起结对编程,他们通过各种对弈游戏决定谁担任Driver谁担任Observer. 今天他们的对弈是在一棵有根树 T 上进行的.小Hi和小Ho轮流进行删除操作,其中小Hi先手. ...

  8. 【Mark】博弈类题目小结(HDU,POJ,ZOJ)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 首先当然要献上一些非常好的学习资料: 基础博弈的小 ...

  9. 博弈论BOSS

    基础博弈的小结:http://blog.csdn.net/acm_cxlove/article/details/7854530 经典翻硬币游戏小结:http://blog.csdn.net/acm_c ...

随机推荐

  1. FileZilla命令行实现文件上传以及CreateProcess实现静默调用

    应用需求:         用户在选择渲染作业时面临两种情况:一是选择用户远程存储上的文件:二是选择本地文件系统中的文件进行渲染.由于渲染任务是在远程主机上进行的,实际进行渲染时源文件也是在ftp目录 ...

  2. Java递归流程

    递归二字顾名思义就是:递过去,归回来.所以我索性叫它做有借有还吧. 下面的例子由c而来: public class Main {                                publ ...

  3. ubuntu14.04使用wubi安装出错

    使用wubi安装后,进入系统是总是提示/分区加载异常,无法正常进入系统. 参考解决方案来自 http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6 ...

  4. C#结构内存布局介绍

    转载:http://www.csharpwin.com/csharpspace/10455r2800.shtml 本来打算写一篇文章,详细地讨论一下结构的内存布局,但是想了下,跟路西菲尔的这篇文章也差 ...

  5. Android 设计随便说说

    我曾经搞过应用程序的设计,当时只是读了半本宝典<重构...>,现在看来就这半本九阴真经,收益甚多啊 .再加上这现年工作上的印证,基本上可以拿出喷一下了.当然现在看来当年的项目设计真是很烂了 ...

  6. 数据库性能高校:CPU使用过高(上)

    CPU使用率过高问题很容易被发现,但是诊断却不是很容易.CPU使用过高很多时候会成为其它问题的替罪羊,所以在确认和故障诊断时要抽丝剥茧. 调查CPU压力 三个主要的工具:性能监视器,SQLTrace, ...

  7. Cisco AnyConnect “Failed to initialize connection subsystem”的解决方案

    Per Cisco: Microsoft has released a fix-it patch providing a workaround for this issue. See KB# 3023 ...

  8. java常用正则表达式

    1.邮编 public static final String POSTAL_CODE = "^\\d{6}$"; 2. email(支持中文域名邮箱) 正则表达式  public ...

  9. IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法

    转载自:http://blog.csdn.net/swingpyzf/article/details/16972351 纠偏也可参考:http://www.2cto.com/kf/201310/253 ...

  10. PC110304/UVA850

    这题目WA了好几次,主要是我没有理解清楚No solution.这个情况. 如果在match原文做好了,基本map一下就能过了. 与原句match的条件就是: 1.出现了26个字母 2.该空格的地方要 ...