【题目】

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

  1. 2
  2. 2 1
  3. 1 2
  4. 4 4
  5. 1 2
  6. 2 3
  7. 2 4
  8. 3 4

Sample Output

  1. Sally

【题意】

【分析】

  这道题在贾志豪的论文《组合游戏略述——浅谈SG游戏的若干拓展及变形》中有。

  于是做这题的时候我花了很长时间苦思冥想、然后花了很长时间看题解看不懂,然后花了很长时间理解整个过程。表示脑子不够用啊。其实一开始那一步,就是变成很多棵根节点只有一个孩子的树的子游戏,把他们异或起来,这一步我是想到的。但是对于子游戏的sg值的求法和环的处理我不是很明白。先说环,偶环是无用的,因为别人做什么我就做什么。对于奇环,我们给他留一个点就好了,同样是别人做什么我就做什么。其实这个思路很常规了,在扫楼梯等题目中已经多次用到,但是我还是没想到这个层面(或许是因为一开始看错题了,不知道环只会在尾部吧)。最难的就是对于一棵树(含边u->v以及以v为根的子树组成的树),它的sg等于v这棵树的sg+1。这个的证明用到了数学归纳法(貌似,就是先说明在数据较小的情况下是成立的,以小推大再推到全部),具体看论文吧,就这个点比较难,需要好好理解。

  

  算出每棵树的sg后,再按NIM游戏异或即可。

代码如下:(巨巨巨巨丑)

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<stack>
  8. using namespace std;
  9. #define Maxn 110
  10. #define Maxm 110
  11. #define Maxk 550
  12.  
  13. struct node
  14. {
  15. int x,y,next,p;
  16. }t[Maxk*];int len;
  17. int first[Maxn*],fa[Maxn*],dep[Maxn*];
  18. int n,m;
  19.  
  20. bool vis[Maxn],mark[Maxn];
  21.  
  22. void ins(int x,int y)
  23. {
  24. t[++len].x=x;t[len].y=y;t[len].p=;
  25. t[len].next=first[x];first[x]=len;
  26. }
  27.  
  28. stack<int > s;
  29.  
  30. int ffind(int x,int f)
  31. {
  32. vis[x]=;
  33. dep[x]=dep[f]+;
  34. //s.push(x);
  35. int now=;
  36. for(int i=first[x];i;i=t[i].next) if(t[i].p==)
  37. {
  38. int y=t[i].y;
  39. t[i].p=;t[(i%==)?i-:i+].p=;
  40. if(vis[y])
  41. {
  42. t[i].p=-,t[(i%==)?i-:i+].p=-;
  43. if((dep[x]-dep[y]+)%==) ins(y,++m);
  44. return y;
  45. }
  46. else if(now=ffind(y,x)) t[i].p=-,t[(i%==)?i-:i+].p=-;
  47. }
  48. if(x==now) return ;
  49. return now;
  50. }
  51.  
  52. int get_sg(int x,int f)
  53. {
  54. int ans=;
  55. for(int i=first[x];i;i=t[i].next) if(t[i].y!=f&&t[i].p!=-)
  56. {
  57. ans^=(get_sg(t[i].y,x)+);
  58. }
  59. return ans;
  60. }
  61.  
  62. int main()
  63. {
  64. while(scanf("%d",&n)!=EOF)
  65. {
  66. int ans=;
  67. while(n--)
  68. {
  69. int k;
  70. scanf("%d%d",&m,&k);
  71. memset(first,,sizeof(first));
  72. len=;
  73. for(int i=;i<=k;i++)
  74. {
  75. int x,y;
  76. scanf("%d%d",&x,&y);
  77. ins(x,y);ins(y,x);
  78. }
  79. memset(vis,,sizeof(vis));
  80. memset(mark,,sizeof(mark));
  81. dep[]=;
  82. ffind(,);
  83. ans^=get_sg(,);
  84. }
  85. if(ans==) printf("Harry\n");
  86. else printf("Sally\n");
  87. }
  88. return ;
  89. }

[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. getViewById和getLayoutInflater().inflate的用法

    getViewById和getLayoutInflater().inflate得用法 1.什么是LayoutInflaterThis class is used to instantiate layo ...

  2. LabVIEW设计模式系列——各种各样的状态机

  3. request对象

    Servlet三大域对象的应用 request.session.application(ServletContext) 请求转发器: public void doGet(HttpServletRequ ...

  4. 菱形实现气泡Bubble,菱形画箭头,菱形画三角形

    菱形实现气泡Bubble,菱形画箭头,菱形画三角形 >>>>>>>>>>>>>>>>>>&g ...

  5. PHP ajax实现数组返回

    首先,我想要实这样一个功能, 当选择一个下拉框时,让其它三个文本框得到从服务器上返回的值!也就把返回的值,赋给那三个文本框! 我用的是jquery+php!! 由于我前台,后台,js,数据库采用的都是 ...

  6. Repeater为空时显示“暂无数据”,很方便实用方法

    Repeater为空时显示“暂无数据”,很方便实用方法 <FooterTemplate>   <asp:Label ID="lblEmptyZP" Text=&q ...

  7. 国内最简单的短视频SDK

    最近阿里百川和趣拍一起合作推出了一个短视频SDK.之前很多厂商可能都是用的Vitamio的短视频SDK.之后我考察过,也做过一些调查,发现Vitamio真的奇贵无比,屌丝公司根本用不起,阿里和趣拍这下 ...

  8. 使用微软企业库5.0提供的unity配置解藕系统demo(源码)

    最近公司集50多号开发人员的人力围绕一个系统做开发,框架是免不了要统一的,公司提供的架构,利于分工合作,便于维护,扩展,升级,其中使用了到微软的企业库来解藕系统,只是因为框架封装,于是在网上学习了一个 ...

  9. 关于shell脚本编程的10个最佳实践

    每一个在UNIX/Linux上工作的程序员可能都擅长shell脚本编程.但大家解决问题的方式却不尽相同,这要取决于对专业知识的掌握程度.使 用命令的种类.看待问题的方式等等.对于那些处在shell脚本 ...

  10. mac下安装配置nginx环境

    本文介绍 nginx 在mac上的安装. 我是通过brewhome 来安装的. brew install nginx 一路顺畅. 下面是安装信息. 复制代码 代码如下: hematoMacBook-P ...