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:

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

题意:

  • 有 N个局部联通的图。
  • Harry 和 Sally轮流从图中删边,删去一条边后,不与根节点相连的部分将被移走。Sally为先手。
  • 图是通过从基础树中加一些边得到的。
  • 所有形成的环保证不共用边,且只与基础树有一个公共点。
  • 谁无法移动谁输。

这题与树上删边游戏很像,只不过加上了环

根据克朗原理,一个以x为根的子树可以视为一颗"竹子"

竹子长度为所有分支边数异或和$len[x]=len[v_1] xor len[v_2]...$

一颗竹子上的树删边游戏可以视为Nim游戏,所以SG[len]=len

相当于$SG[x]=SG[v_1] xor SG[v_2]....$

考虑把环转化:(环的根为x)

当环的边数为奇数时:

任意删一条边,那么x的2个支链必定同奇偶

那么SG异或值显然为偶数

那么SG[x]=1,等价与把奇环缩成一个新点

边数为偶数时

SG异或值为奇数

SG[x]=0,等价与把偶环去掉

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 struct Node
 8 {
 9   int next,to;
10 }edge[2001];
11 int head[105],num,SG[105],stack[105],top,n,m,ans;
12 int vis[105];
13 void add(int u,int v)
14 {
15   num++;
16   edge[num].next=head[u];
17   head[u]=num;
18   edge[num].to=v;
19 }
20 void dfs(int x,int pa)
21 {int i,now,cnt,flag;
22   vis[x]=1;
23   stack[++top]=x;
24   flag=0;
25   for (i=head[x];i;i=edge[i].next)
26     {
27       int v=edge[i].to;
28       if (v==pa&&flag==0)
29     {
30       flag=1;
31       continue;
32     }
33       if (vis[v]==1)
34     {
35       now=stack[top];cnt=1;
36       while (now!=v)
37         {
38           cnt++;
39           vis[now]=0;
40           now=stack[--top];
41         }
42       if (cnt%2==1)
43         SG[v]^=1;
44     }
45       else if (vis[v]==-1)
46     {
47       dfs(v,x);
48       if (vis[v]) SG[x]^=(1+SG[v]);
49     }
50     }
51   if (vis[x]) top--;
52 }
53 int main()
54 {int N,i,j,u,v;
55   while (cin>>N)
56     {
57       ans=0;
58       for (j=1;j<=N;j++)
59     {
60       scanf("%d%d",&n,&m);
61       memset(head,0,sizeof(head));
62       memset(SG,0,sizeof(SG));
63       memset(vis,-1,sizeof(vis));
64       num=0;top=0;
65       for (i=1;i<=m;i++)
66         {
67           scanf("%d%d",&u,&v);
68           add(u,v);
69           add(v,u);
70         }
71       dfs(1,0);
72       ans^=SG[1];
73     }
74       if (ans) printf("Sally\n");
75       else printf("Harry\n");
76     }
77 }

原文地址:https://www.cnblogs.com/Y-E-T-I/p/8438446.html

时间: 2024-10-23 13:28:33

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(树上的删边游戏)

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 tre

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

POJ Big Christmas Tree(最短的基础)

Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the edge)尽量的小.转换后就是求根节点到每一个节点的距离最短,也就是最短路. 生成树可能会超时.我没试过.然后,求解最短路要用优化的解法不然会超时.最后的答案就是:sum = w[1] * dist[1] + w[2] * dist[2] + ..... w[n] * dist[n].能够自己推推例

POJ Big Christmas Tree(基础最短路)

Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the edge)尽量的小.转换后就是求根节点到每个节点的距离最短,也就是最短路.生成树可能会超时,我没试过.然后,求解最短路要用优化的解法不然会超时.最后的答案就是:sum = w[1] * dist[1] + w[2] * dist[2] + ..... w[n] * dist[n].可以自己推推样例就

POJ【数论/组合/博弈论】

 POJ[数论/组合/博弈论]题目列表 POJ[数论/组合/博弈论]题目列表 原来的列表比较水,今天换了一个难一些的列表,重新开始做~ 红色的代表已经AC过,蓝色的代表做了但是还没过.这句话貌似在我空间里的每份列表里都有额. 博弈论 POJ 2234 Matches Game POJ 2975 Nim POJ 2505 A multiplication game POJ 1067 取石子游戏 POJ 2484 A Funny Game POJ 2425 A Chess Game POJ 29

Part.4【博弈论】

---恢复内容开始--- 不要问我为什么突然跳到Part.4,我懒得解释. 在蔡大神的论文+讲解和HZW的题库下,自己大概是明白什么是博弈论的皮毛了吧. 先说SG定理吧. 对于游戏中的状态,我们给每个状态定义一个必胜态和必败态.区别在于前者可以通过一次操作到达必败态,但后者无法做到(后者在一次操作后所能到达的状态全部都为必胜态) 接着引进SG函数,每个状态都有一个SG值,这个值由它所能到达的状态的SG值决定.(这里的所能到达的状态指的是经过一次操作能到达的状态,下同) SG值有以下性质: SG值

(转载)--SG函数和SG定理【详解】

在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点,处于此情况下,双方操作均正确的情况下必胜. 必胜点和必败点的性质: 1.所有终结点是 必败点 P .(我们以此为基本前提进行推理,换句话说,我们以此为假设) 2.从任何必胜点N 操作,至少有一种方式可以进入必败点 P. 3.无论如何操作,必败点P 都只能进入 必胜点 N. 我们研究必胜点和必败点的目的时间为题进行简化,有助于

SG函数和SG定理【详解】

在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点,处于此情况下,双方操作均正确的情况下必胜. 必胜点和必败点的性质: 1.所有终结点是 必败点 P .(我们以此为基本前提进行推理,换句话说,我们以此为假设) 2.从任何必胜点N 操作,至少有一种方式可以进入必败点 P. 3.无论如何操作,必败点P 都只能进入 必胜点 N. 我们研究必胜点和必败点的目的时间为题进行简化,有助于