poj 3710 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后的异或和。
二 拥有奇数条边的环可简化为一条边,偶数条边的环可以简化为一个点。
【代码】
#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(树上的删边游戏)的更多相关文章
- POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)
题目链接 \(Description\) 给定n棵"树",每棵"树"的节点可能"挂着"一个环,保证没有环相交,且与树只有一个公共点. 两人轮 ...
- POJ 3710 Christmas Game#经典图SG博弈
http://poj.org/problem?id=3710 (说实话对于Tarjan算法在搞图论的时候就没搞太懂,以后得找时间深入了解) (以下有关无向图删边游戏的资料来自论文贾志豪<组合游戏 ...
- POJ 3710 Christmas Game [博弈]
题意:略. 思路:这是个删边的博弈游戏. 关于删边游戏的预备知识:http://blog.csdn.net/acm_cxlove/article/details/7854532 学习完预备知识后,这一 ...
- poj 3710 Christmas Game【博弈论+SG】
也就是转换到树形删边游戏,详见 https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html #include<iostream> ...
- poj 3710 Christmas Game 博弈论
思路:首先用Tarjan算法找出树中的环,环为奇数变为边,为偶数变为点. 之后用博弈论的知识:某点的SG值等于子节点+1后的异或和. 代码如下: #include<iostream> #i ...
- POJ 3710 Christmas Game
知识储备: 解决办法(奇偶去环): (1) 对于长度为奇数的环,去掉其中任意一个边之后,剩下的 两个链长度同奇偶,抑或之后的 SG 值不可能为奇数,所 以它的 SG 值为 1: (2) 对于长度为 ...
- POJ 3710 无向图简单环树上删边
结论题,这题关键在于如何转换环,可以用tarjan求出连通分量后再进行标记,也可以DFS直接找到环后把点的SG值变掉就行了 /** @Date : 2017-10-23 19:47:47 * @Fil ...
- POJ Christmas Game [树上删边游戏 Multi-SG]
传送门 题意: 有N 个局部联通的图.Harry 和Sally 轮流从图中删边,删去一条边后,不与根节点相连的部分将被移走.Sally 为先手.图是通过从基础树中加一些边得到的.所有形成的环保证不共用 ...
- 【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 ...
随机推荐
- .NET设计模式(9):桥接模式(Bridge Pattern)
.NET设计模式(9):桥接模式(Bridge Pattern) 桥接模式(Bridge Pattern) --.NET设计模式系列之九 年月 实现代码如下:..所谓抽象和实现沿着各自维度的变 ...
- Http请求通信(工具类)
Http请求通信(工具类) 异步消息处理流程是: 首先需要在主线程当中创建一个Handle对象,并重写handlerMessage()方法. 然后当子线程中需要进行UI操作时,就创建一个Message ...
- insert一句话实现插入一条数据并且返回这条数据的某列
insert into [table] output inserted.columnName values();
- .NET下的加密解密大全(1): 哈希加密
.NET有丰富的加密解密API库供我们使用,本博文总结了.NET下的Hash散列算法,并制作成简单的DEMO,希望能对大家有所帮助. MD5[csharp]using System; using Sy ...
- Ajax结合Js操作灵活操作表格
Table页面: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- C++Primer学习笔记(二、基础)
1.两种初始化方式,直接初始化语法更灵活,且效率更高. ); // 直接初始化 direct-initialization ; // 赋值初始化 copy-initialization 2.const ...
- iOS textfield实现一行的数字限制,超出进行弹框
步骤一:添加textfield协议‘ @interface LsGeXingQianMingVC ()<UITextFieldDelegate> 步骤2:设置代理 _GeXingQianM ...
- ERROR ITMS-90167: "No .app bundles found in the package"
http://stackoverflow.com/questions/37838487/error-itms-90167-no-app-bundles-found-in-the-package 简单说 ...
- 前端开发构建工具gulp的安装使用
曾几何时还在使用grunt作为前端的构建工具,直到有一天同事向我推荐了gulp,在这里博主将不讨论gulp与grunt各自优势的比较,只为大家介绍gulp如何安装和使用. Gulp 是用 nodejs ...
- 中文翻译:pjsip文档(四)之ICE Session的使用方法
1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...