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. declare-styleable:自定义控件的属性

    http://www.cnblogs.com/jisheng/archive/2013/01/10/2854891.html 在使用过程中, 1 TypedArray a = getContext() ...

  2. setTimeout 和 setInterval区别

    setTimeout和setIntelval都有定时的功能!!!取消定时功能的时候,都有对应的clearTimeout以及clearInterval与之对应. 但是他们之间是有区别的! setTime ...

  3. Perl连接Sqlite数据库

    Sqlite是一个小巧的嵌入式关系型数据库,几乎可以嵌入所有编程语言,特别是C,C++,PHP,Perl等.这里就介绍如何用Perl连接并操作Sqlite数据库. use DBI; # perl用以操 ...

  4. shiro认证

    一.通过ini文件初始化一个用户 1.通过ini配置文件创建securityManager2.调用subject.login方法主体提交认证,提交的token3.securityManager进行认证 ...

  5. sql server获取当前日期

    SqlServer中得到当前日期(convert函数,getdate函数)函数GETDATE()的返回值在显示时只显示到秒.实际上,SQL Sever内部时间可以精确到毫秒级(确切地说,可以精确到3. ...

  6. prime,素数的判断——c语言

    输入一个数a,求他是否是素数(用函数) 程序: #include<stdio.h> int prime(int a)-----------------------------------/ ...

  7. java.net.ServerSocket和java.net.Socket

    个人博客地址:http://www.cnblogs.com/wdfwolf3/ java.net.ServerSocket 1.构造函数 a.ServerSocket() 创建一个无连接的server ...

  8. UVA10142/PC110108Australian Voting

    UVA10142/PC110108Australian Voting 10142 Australian Voting Accepted C++11 0.769 2014-02-11 05:01:20 ...

  9. mysql修改字符集 转载

    查看编码:    show variables like 'collation_%';    show variables like 'character_set_%';    修改:    MySQ ...

  10. Debian6安装XP系统

    1.下载一键包,必须要在 root目录 下执行,可以先用 pwd 命令查看当前所在目录.执行命令:wget http://d.yxlgh.com/vps/zmdebian6xp.sh 2.执行 zmd ...