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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

【思路】

       树上的删边游戏

       一 叶子结点的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 }

 

以上是关于poj 3710 Christmas Game(树上的删边游戏)的主要内容,如果未能解决你的问题,请参考以下文章

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

poj3710 Christmas Game

POJ 3710 Christmas Game#经典图SG博弈

POJ 3710:Christmas Game

[CF1498F]Christmas Game

poj 3013 Big Christmas Tree