【POJ3710】Christmas Game (博弈-树上的删边问题)
【题目】
|
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. Output For each test case, output the name of the winner. Sample Input 2 Sample Output Sally |
【题意】

【分析】
这道题在贾志豪的论文《组合游戏略述——浅谈SG游戏的若干拓展及变形》中有。
于是做这题的时候我花了很长时间苦思冥想、然后花了很长时间看题解看不懂,然后花了很长时间理解整个过程。表示脑子不够用啊。其实一开始那一步,就是变成很多棵根节点只有一个孩子的树的子游戏,把他们异或起来,这一步我是想到的。但是对于子游戏的sg值的求法和环的处理我不是很明白。先说环,偶环是无用的,因为别人做什么我就做什么。对于奇环,我们给他留一个点就好了,同样是别人做什么我就做什么。其实这个思路很常规了,在扫楼梯等题目中已经多次用到,但是我还是没想到这个层面(或许是因为一开始看错题了,不知道环只会在尾部吧)。最难的就是对于一棵树(含边u->v以及以v为根的子树组成的树),它的sg等于v这棵树的sg+1。这个的证明用到了数学归纳法(貌似,就是先说明在数据较小的情况下是成立的,以小推大再推到全部),具体看论文吧,就这个点比较难,需要好好理解。

算出每棵树的sg后,再按NIM游戏异或即可。
代码如下:(巨巨巨巨丑)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define Maxn 110
#define Maxm 110
#define Maxk 550 struct node
{
int x,y,next,p;
}t[Maxk*];int len;
int first[Maxn*],fa[Maxn*],dep[Maxn*];
int n,m; bool vis[Maxn],mark[Maxn]; void ins(int x,int y)
{
t[++len].x=x;t[len].y=y;t[len].p=;
t[len].next=first[x];first[x]=len;
} stack<int > s; int ffind(int x,int f)
{
vis[x]=;
dep[x]=dep[f]+;
//s.push(x);
int now=;
for(int i=first[x];i;i=t[i].next) if(t[i].p==)
{
int y=t[i].y;
t[i].p=;t[(i%==)?i-:i+].p=;
if(vis[y])
{
t[i].p=-,t[(i%==)?i-:i+].p=-;
if((dep[x]-dep[y]+)%==) ins(y,++m);
return y;
}
else if(now=ffind(y,x)) t[i].p=-,t[(i%==)?i-:i+].p=-;
}
if(x==now) return ;
return now;
} int get_sg(int x,int f)
{
int ans=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f&&t[i].p!=-)
{
ans^=(get_sg(t[i].y,x)+);
}
return ans;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int ans=;
while(n--)
{
int k;
scanf("%d%d",&m,&k);
memset(first,,sizeof(first));
len=;
for(int i=;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ins(x,y);ins(y,x);
}
memset(vis,,sizeof(vis));
memset(mark,,sizeof(mark));
dep[]=;
ffind(,);
ans^=get_sg(,);
}
if(ans==) printf("Harry\n");
else printf("Sally\n");
}
return ;
}
[POJ3710]
2016-04-21 12:58:40
【POJ3710】Christmas Game (博弈-树上的删边问题)的更多相关文章
- poj 3710 Christmas Game(树上的删边游戏)
Christmas Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1967 Accepted: 613 Des ...
- POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)
题目链接 \(Description\) 给定n棵"树",每棵"树"的节点可能"挂着"一个环,保证没有环相交,且与树只有一个公共点. 两人轮 ...
- 【BZOJ 2688】 2688: Green Hackenbush (概率DP+博弈-树上删边)
2688: Green Hackenbush Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 42 Solved: 16 Description ...
- POJ3710 Christmas Game 博弈论 sg函数 树的删边游戏
http://poj.org/problem?id=3710 叶子节点的 SG 值为0:中间节点的SG值为它的所有子节点的SG值加1后的异或和. 偶环可以视作一个点,奇环视为一条边(连了两个点). 这 ...
- POJ 3710 Christmas Game [博弈]
题意:略. 思路:这是个删边的博弈游戏. 关于删边游戏的预备知识:http://blog.csdn.net/acm_cxlove/article/details/7854532 学习完预备知识后,这一 ...
- poj3710 Christmas Game
题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...
- hihocoder1545 : 小Hi和小Ho的对弈游戏(树上博弈&nim博弈)
描述 小Hi和小Ho经常一起结对编程,他们通过各种对弈游戏决定谁担任Driver谁担任Observer. 今天他们的对弈是在一棵有根树 T 上进行的.小Hi和小Ho轮流进行删除操作,其中小Hi先手. ...
- 【Mark】博弈类题目小结(HDU,POJ,ZOJ)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 首先当然要献上一些非常好的学习资料: 基础博弈的小 ...
- 博弈论BOSS
基础博弈的小结:http://blog.csdn.net/acm_cxlove/article/details/7854530 经典翻硬币游戏小结:http://blog.csdn.net/acm_c ...
随机推荐
- WPF中将DataGrid导出Excel
int number = 1; private void MenuItem_Click(object sender, RoutedEventArgs e) { #region string path ...
- 11.10 noip模拟试题
1.第K小数 (number.cpp/c/pas) [问题描述] 有两个正整数数列,元素个数分别为N和M.从两个数列中分别任取一个数 相乘,这样一共可以得到N*M个数,询问这N*M个数中第K小数是多少 ...
- css3新增加的选择器
css3新增加的选择器 一.属性选择器: E[attr] 只要有属性名E[attr=value] 属性名=属性值E[attr~=blue] 包含这个blue整个单词就可以E[attr^=c] 以这个字 ...
- Animating Layout Changes(展开收起)
原文地址:https://developer.android.com/training/animation/layout.html#add (1)设置布局文件: <LinearLayout an ...
- Git查看、删除、重命名远程分支和tag
这篇文章记录我在使用git的过程中碰到远程分支和tag的相关内容,提纲: 查看远程分支 删除远程分支和tag 删除不存在对应远程分支的本地分支 重命名远程分支 把本地tag推送到远程 获取远程tag ...
- hibernate逆向工程生成的实体映射需要修改
根据实际情况进行修改,主要2处,注释的位置<!-- 把catalog="platform"删掉 -->,<!-- 替换为native --> <? ...
- MyBatis的学习总结五:调用存储过程【参考】
一.创建存储过程 存储过程的目的:统计edi_test_task 正在运行的任务和非运行的任务 CREATE DEFINER=`root`@`%` PROCEDURE `edihelper`.`SP_ ...
- IOS多线程知识总结/队列概念/GCD/串行/并行/同步/异步
进程:正在进行中的程序被称为进程,负责程序运行的内存分配;每一个进程都有自己独立的虚拟内存空间: 线程:线程是进程中一个独立的执行路径(控制单元);一个进程中至少包含一条线程,即主线程. 队列:dis ...
- GCDTimer
#import <Foundation/Foundation.h> @interface JKTimerManager : NSObject + (instancetype)sharedT ...
- 适合自己的vim配置文件
主要用来写c++的:clang-completer这个是单独安装的,其他的都采用的vundle安装完成. clang-completer:只在centos7.2上安装成功过,6.4上失败了.先要安装一 ...