HDU 3904 A tree game题解
来源:互联网 发布:手机淘宝如何找星店 编辑:程序博客网 时间:2024/06/17 06:49
【题意】:
给定一个以1号节点为根的含N个节点的树,Alice先手,Bob后手玩一个游戏:轮流删去树中的边,之后将与根断开了联系的部分去除。无法继续删边者为负。
【分析】:
这是树的删边博弈游戏,首先先考虑更加简单的链的删边博弈游戏。
链的删边博弈游戏游戏规则:对于一条链,两人轮流删边,脱离根的部分去除,没边可删的人即输。考虑其sg值。
于是乎,很明显,这成了一个Nim游戏的组合。
(上述资料原版来自:【博弈】无向图删边游戏)
【代码】:
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;#define MAX 200001#define MAXN 100001struct TREENODE{int f,t,next;}a[MAX];int DATA,N,last[MAXN],tot;bool vis[MAXN];void add(int from,int to){a[++tot].t=to;a[tot].next=last[from];last[from]=tot;a[++tot].t=from;a[tot].next=last[to];last[to]=tot;}int work(int now){int ans=0;for(int i=last[now];i;i=a[i].next){ int to=a[i].t; if(!vis[to]) { vis[to]=true; ans^=(work(to)+1); }}return ans;}int main(){ scanf("%d",&DATA); for(int data=1;data<=DATA;data++) { tot=0; memset(last,0,sizeof(last)); memset(vis,false,sizeof(vis)); scanf("%d",&N); for(int i=1;i<N;i++) { int F,T; scanf("%d%d",&F,&T); add(F,T); } vis[1]=true; if(!work(1)) printf("Bob\n"); else printf("Alice\n");}return 0;}
0 0
- HDU 3904 A tree game题解
- HDU 3094 A tree game
- HDU 3094 A tree game
- hdu 3094——A tree game
- HDU 3904 A tree game(树的删边游戏,树形图博弈)
- HDU 3094 A tree game 树的删边游戏
- HDU 3094 A tree game(树上删边游戏)
- hdu 3094 A tree game (树形删边游戏)
- hdu3094 A tree game
- [hdu-3622] Bomb Game题解
- programming-challenges A multiplication game (110505) 题解
- POJ 1740[A New Stone Game] 题解
- hdu 3094 A tree game 2009 Multi-University Training Contest 18 - Host by ECNU
- hdu 3094 A tree game (博弈 树的删边问题)
- HDU 3744 - A Runing Game
- hdu 3744 A Runing Game
- HDU 1517 A Multiplication Game
- hdu 1517 A Multiplication Game
- jQuery表单input文本框默认说明文字获得焦点后消失效果
- php中的short_open_tag的作用
- Android Studio 解决方法数超过65536,导致dex无法生成的问题
- Java网络爬虫抓取新浪微博个人微博记录
- ORA-28001: the password has expired
- HDU 3904 A tree game题解
- PHP 5.0 的 新特性
- Code 阅读神器
- C++中使用TinyXML2
- 备忘录模式
- 网站发布后在IIS上定时执行任务
- Log4j.properties配置
- Blob、FileReader全面解析
- 关于C++ const 的全面总结