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

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

POJ Founder Monthly Contest – 2008.12.28, Yaojinyu

【思路】

树上的删边游戏

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

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

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

【代码】

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5
 6 const int N = 1000+10;
 7
 8 int n,m,T;
 9 int cnt,to[N],next[N],head[N];
10
11 int w[N],s[N],top,vis[N],ve[N];
12 void insert(int u,int v) {
13     to[++cnt]=v;next[cnt]=head[u];head[u]=cnt;
14     to[++cnt]=u;next[cnt]=head[v];head[v]=cnt;
15 }
16
17 int dfs(int x) {
18     vis[x]=1;
19     int ans=0;
20     s[++top]=x;
21     for(int i=head[x];i;i=next[i])
22         if(!ve[i]) {
23             ve[i]=1;ve[i^1]=1;
24             int temp;
25             if(!vis[to[i]])temp=dfs(to[i])+1;
26             else {
27                 int q=s[top--];
28                 while(q!=to[i])
29                     w[q]=1 , q=s[top--];
30                 ++top;
31                 return 1;
32             }
33             if(w[to[i]]) ans^=(temp)%2;
34             else ans^=temp;
35         }
36     return ans;
37 }
38
39 int main() {
40     while(scanf("%d",&T)==1) {
41         int ans=0;
42         while(T--) {
43             memset(head,0,sizeof(head));
44             memset(next,-1,sizeof(next));
45             memset(vis,0,sizeof(vis));
46             memset(ve,0,sizeof(ve));
47             memset(w,0,sizeof(w));
48             top=0;cnt=1;
49             scanf("%d%d",&n,&m);
50             int u,v;
51             for(int i=0;i<m;i++) {
52                 scanf("%d%d",&u,&v);
53                 insert(u,v);
54             }
55             ans^=dfs(1);
56         }
57         if(ans)puts("Sally");
58         else puts("Harry");
59     }
60     return 0;
61 }
时间: 2024-08-08 12:33:56

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

POJ 3710 Christmas Game#经典图SG博弈

http://poj.org/problem?id=3710 (说实话对于Tarjan算法在搞图论的时候就没搞太懂,以后得找时间深入了解) (以下有关无向图删边游戏的资料来自论文贾志豪<组合游戏略述--浅谈SG游戏的若干拓展及变形>) 首先,对于无向图的删边游戏有如下定理性质: 1.(Fushion Principle定理)我们可对无向图做如下改动:将图中的任意一个偶环缩成一个新点,任意一个奇环缩成一个新点加一个新边:所有连到原先环上的边全部改为与新点相连:这样的改动不影响图的SG值. 2.(

POJ 3710 Christmas Game

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: S

POJ 3710

树的删边游戏.. 由于题目的特殊性,我们只需计算环的边数值.若为偶环,则直接把环的根节点置0.若为奇环,则留下一条边与根结点相连,并那它们的SG置0: 注意的是,两个点也可构成环,因为允许重边.所以,我们只需求点双连通分量,并判断分量中边的数量即可.然后DFS求树的SG值. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 6 using namespace std; 7 8 const

【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 Submission(s): 510    Accepted Submission(s): 256 Problem Description PP and QQ were playing games at Christmas Eve. They drew some Christmas trees on

POJ 2468 Apple Tree 树上瞎搞分组背包

昨晚Debug了好久始终找不出哪里错了,今早再一看发现自己已荣升逗比Beta 2.0 Version. 个人感觉此题为HDU 4003 的弱化版. 把每棵子树都看成一类商品,在每类商品中至多选一件.则问题转化为最基本的分组背包问题. dp[s][c][k] c == 1时,表示在s结点不返回时走K的最大收益,c == 0时,表示在s结点重新返回时走k步的最大收益. 可以dfs从底到顶更新dp.值得一提的是,要保证每个结点的dp[s][c][k]只会被dfs一次,即不能重复更新. 因为重复的dfs

hdu 5299(树上删边游戏) Circles Game

题意: 给你一些圆圈的圆心坐标和半径,保证这些圆圈是包含或者相离的.现在两个人做博弈操作,拿掉一个圆圈,然后这个圆圈所包含的都要移除掉.两个人一直拿下去,直到某个人找不到一个可以移除的圈他就输了. 思路 学习的姿势.这是一个树上删边游戏模型: 首先按照半径升序排序,然后对于每一个圆找第一个包含它的圆,然后连一条边.建树完成. 叶子节点的SG值为0; 中间节点的 SG 值为它的所有子节点的 SG 值加 1 后的异或和; 这里可以建立成森林或者直接一棵树,没有太大区别.建成一棵树估计要方便一些吧.

博弈——无向图删边游戏

关于无向图删边游戏,首先游戏的规则如下: 然后看下最关键的定理: 叶子节点的 SG 值为 0; 中间节点的 SG 值为它的所有子节点的 SG 值 加 1 后的异或和. 精彩证明: 有了这个定理,这个问题就可以轻松用sg函数搞定了. 然后再来看几个变形. 1. 可以发现,得到两个关键性质,直接就可以转变为树上删边游戏了. 2. 对于这种情况只需要不停的找环,然后缩点,变成树即可解决. 本文大部分根据 贾志豪的 <组合游戏略述——浅谈 SG 游戏的若干拓展及变形 >得到.

POJ - 1987 Distance Statistics 树上的分治

题目大意:和poj 1741的那题和类似,求树上节点之间的距离小于等于k的节点对有多少对 解题思路:具体可参考:<分治算法在树的路径问题中的应用--漆子超> 给这题的输入坑了,注意输入,不然会超时 #include<cstdio> #include<vector> #include<algorithm> using namespace std; #define maxn 40010 int vis[maxn], Sum[maxn], d[maxn], dp[

【POJ3710】Christmas Game 博弈 有限制的图上删边游戏(树形删边游戏进化1)

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42671885 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 题意: 一个树图,然后1永远是根,两人轮流删边,不能删者输. 删边限制:只能删跟1连通的边. 树图限制: 它首先是一棵树,然后某些点上可能带一个环 原描述:最开始的图是一颗带有一些简单环的树,然后任何边都至多在一个多边形上.每个多边形最多有一个节点出现在主树上. 给个神犇论文地址: 石家庄二中·贾志豪-- <组合