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 ...
随机推荐
- Android开发手记(22) 传感器的使用
Android的传感器主要包括八大传感器,他们分别是:加速度传感器(accelerometer).陀螺仪(gyroscope).方向传感器(orientation).磁力传感器(magnetic fi ...
- Android - 向服务器发送数据(POST) - HTTPClient.
该篇文章主要说明使用Apache提供的HTTPClient,通过post方式,向服务器发送数据.由于有些东西在 Android - 向服务器发送数据(GET)中提到过,就不再重复. 一,Android ...
- Mybatis的学习总结二:使用Mybatis对表进行CRUD操作【参考】
一.使用Mybatis对表进行CRUD操作------基于XML的实现 1.定义SQL的映射文件 2.在conf.xml中进行注册. 2.创建测试类 [具体过程参考:Mybatis的学习总结一] 二. ...
- iOS-assign、copy 、retain等关键字的含义
iOS中assign.copy .retain等关键字的含义 assign: 简单赋值,不更改索引计数 copy: 建立一个索引计数为1的对象,然后释放旧对象 retain:释放旧的对象,将旧对象的值 ...
- c#yield,IEnumerable,IEnumerator
foreach 在编译成IL后,实际代码如下: 即:foreach实际上是先调用可枚举对象的GetEnumerator方法,得到一个Enumerator对象,然后对Enumerator进行while循 ...
- java封装和多态
封装.集成.多态和抽象是java的基本特征. 封装的第一步就是对类进行组装,即定义一个类,这时候要考虑这个类要有哪些属性.方法等.第二步就是信息的隐藏,这包括访问修饰符.get/set方法和某些特定方 ...
- ecmall 后台导航增加菜单
以我增加的团购为例: languages\sc-gbk\admin 中的default.lang.php 文件中底部增加变量值 'groupbuy'=>'团购', 'groupbuyord ...
- fedora23开发环境搭建手册
chrome安装 [安装chrome教程] nodejs环境搭建 dnf install nodejs dnf install npm sublime text 编辑器安装配置 [fedora安装su ...
- JavaScript学习心得(四)
条件语句 任何事物非真即假. 在JavaScript中,条件判断以下情况为假: false NaN 0 空串 null undefined 在使用相等运算符时,建议将数字写在相等运算符的左边.全等比较 ...
- 越狱开发:用iosOpenDev配置越狱开发环境 编写第一个hello world-b
上网搜索越狱环境搭建,查到iosOpenDev软件,是个很方便的编写插件,而且可以直接用Xcode创建的工具,便开始着手学习如何去安装 集合了网上的方法后,我安装的时候发现: 1.作者最后更新的时间是 ...