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

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 <a, b>. 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

Hint

The sample graph is

Source

【思路】

树上的删边游戏

一 叶子结点的sg为0,中间结点的sg为所有子节点sg值加1后的异或和。

二 拥有奇数条边的环可简化为一条边,偶数条边的环可以简化为一个点。

详见论文:《组合游戏略述——浅谈SG游戏的若干拓展及变形》

【代码】

 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; const int N = +; int n,m,T;
int cnt,to[N],next[N],head[N]; int w[N],s[N],top,vis[N],ve[N];
void insert(int u,int v) {
to[++cnt]=v;next[cnt]=head[u];head[u]=cnt;
to[++cnt]=u;next[cnt]=head[v];head[v]=cnt;
} int dfs(int x) {
vis[x]=;
int ans=;
s[++top]=x;
for(int i=head[x];i;i=next[i])
if(!ve[i]) {
ve[i]=;ve[i^]=;
int temp;
if(!vis[to[i]])temp=dfs(to[i])+;
else {
int q=s[top--];
while(q!=to[i])
w[q]= , q=s[top--];
++top;
return ;
}
if(w[to[i]]) ans^=(temp)%;
else ans^=temp;
}
return ans;
} int main() {
while(scanf("%d",&T)==) {
int ans=;
while(T--) {
memset(head,,sizeof(head));
memset(next,-,sizeof(next));
memset(vis,,sizeof(vis));
memset(ve,,sizeof(ve));
memset(w,,sizeof(w));
top=;cnt=;
scanf("%d%d",&n,&m);
int u,v;
for(int i=;i<m;i++) {
scanf("%d%d",&u,&v);
insert(u,v);
}
ans^=dfs();
}
if(ans)puts("Sally");
else puts("Harry");
}
return ;
}

poj 3710 Christmas Game(树上的删边游戏)的更多相关文章

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

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

  2. POJ 3710 Christmas Game#经典图SG博弈

    http://poj.org/problem?id=3710 (说实话对于Tarjan算法在搞图论的时候就没搞太懂,以后得找时间深入了解) (以下有关无向图删边游戏的资料来自论文贾志豪<组合游戏 ...

  3. POJ 3710 Christmas Game [博弈]

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

  4. poj 3710 Christmas Game【博弈论+SG】

    也就是转换到树形删边游戏,详见 https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html #include<iostream> ...

  5. poj 3710 Christmas Game 博弈论

    思路:首先用Tarjan算法找出树中的环,环为奇数变为边,为偶数变为点. 之后用博弈论的知识:某点的SG值等于子节点+1后的异或和. 代码如下: #include<iostream> #i ...

  6. POJ 3710 Christmas Game

    知识储备: 解决办法(奇偶去环):   (1) 对于长度为奇数的环,去掉其中任意一个边之后,剩下的 两个链长度同奇偶,抑或之后的 SG 值不可能为奇数,所 以它的 SG 值为 1: (2) 对于长度为 ...

  7. POJ 3710 无向图简单环树上删边

    结论题,这题关键在于如何转换环,可以用tarjan求出连通分量后再进行标记,也可以DFS直接找到环后把点的SG值变掉就行了 /** @Date : 2017-10-23 19:47:47 * @Fil ...

  8. POJ Christmas Game [树上删边游戏 Multi-SG]

    传送门 题意: 有N 个局部联通的图.Harry 和Sally 轮流从图中删边,删去一条边后,不与根节点相连的部分将被移走.Sally 为先手.图是通过从基础树中加一些边得到的.所有形成的环保证不共用 ...

  9. 【HDU 3590】 PP and QQ (博弈-Anti-SG游戏,SJ定理,树上删边游戏)

    PP and QQ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. SQL查询一些浅薄的结论

    一些简单的测试结论 在本机经过一些简单的测试,记录数6W条,得出以下结论,不同的硬件环境和数据记录数,可能会有不一样的结论 1.in, or, exists, like, not in , not e ...

  2. Java 中文件下载的几种应用

    public HttpServletResponse download(String path, HttpServletResponse response) { try { // path是指欲下载的 ...

  3. java_反射_及其简单应用(2016-11-16)

    话不多说直接上代码 接口: package bean; /** * user接口 */ public interface User { public String getName(); public ...

  4. Fast Report Data Filter

    使用Data Filter两种方式:一种是 直接在Filter 属性里写表达式 ,另外一种就是在beforePrint 事件里写方法. 今天开发时遇到了一个Filter的问题,不知道是不是fast r ...

  5. 一天学完UFLDL

    学习UFLDL笔记 第一节 神经网络 神经元长这样 大写W看着有点不习惯.. 激活函数, 就是上面式子中的f. 可以选 sigmoid函数(或者叫 logistic回归,对数几率函数),反正就是这样一 ...

  6. 三角网格(Triangle Mesh)的理解

    最简单的情形,多边形网格不过是一个多边形列表:三角网格就是全部由三角形组成的多边形网格.多边形和三角网格在图形学和建模中广泛使用,用来模拟复杂物体的表面,如建筑.车辆.人体,当然还有茶壶等.图14.1 ...

  7. php中如何实现网上商城用户历史浏览记录的代码

    /如是COOKIE 里面不为空,则往里面增加一个商品ID if (!empty($_COOKIE['SHOP']['history'])){ //取得COOKIE里面的值,并用逗号把它切割成一个数组 ...

  8. PHP 单一入口

    单一入口概述 单一入口的应用程序就是说用一个文件处理所有的HTTP请求,例如不管是列表页还是文章页,都是从浏览器访问index.php文件,这个文件就是这个应用程序的单一入口. 打个比方,大家都要上W ...

  9. mysql建表出现Timestamp错误

    mysql建表时如果有两个或以上的字段为Timestamp,那么可能会出现如下错误: Incorrect table definition; there can be only one TIMESTA ...

  10. Fedora 21 中添加及更新源的命令

    原文: Fedora 21 中添加及更新源的命令 fedora的软件源信息文件(*.repo)都是放在 /etc/yum.repos.d 目录下的.可以通过# ls -l /etc/yum.repos ...