HDU 3094 A tree game
Given
is a tree on N vertices, The vertices are numbered from 1 to N. vertex 1
represents the root. There are N-1 edges. Players alternate in making
moves, Alice moves first. A move consists of two steps. In the first
step the player selects an edge and removes it from the tree. In the
second step he/she removes all the edges that are no longer connected to
the root. The player who has no edge to remove loses.
You may assume that both Alice and Bob play optimally.
Each
test case begins with a line containing an integer N
(1<=N<=10^5), the number of vertices,The following N-1 lines each
contain two integers I , J, which means I is connected with J. You can
assume that there are no loops in the tree.
根据克朗原理,一个分支可以转化为以这个点为根,以各个分支边数量的异或和为长度的竹子
实际上等价于
$SG[x]=(SG[v1]+1)xor(SG[v2]+1)xor(SG[v3]+1)...$
加1是因为要考虑x到v的边
然后递归处理SG函数就行了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Node
{
int next,to;
}edge[];
int head[],num,SG[],n;
void add(int u,int v)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
}
void dfs(int x,int pa)
{int i;
SG[x]=;
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa) continue;
dfs(v,x);
SG[x]^=(SG[v]+);
}
}
int main()
{int T,i,u,v;
cin>>T;
while (T--)
{
memset(head,,sizeof(head));
num=;
scanf("%d",&n);
for (i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs(,);
if (SG[])
printf("Alice\n");
else printf("Bob\n");
}
}
从某一棵树上删除一条边,
同时删去所有在删除边后不再与根相连的部分。
双方轮流进行,
无法再进行删除者判定为失败
HDU 3094 A tree game的更多相关文章
- hdu 3094 A tree game 树上sg
A tree game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- hdu 3094 A tree game 博弈论
思路: 叶子节点的SG值为0:中间节点的SG值为它的所有子节点的SG值加1 后的异或和. 详见贾志豪神牛的论文:组合游戏略述 ——浅谈SG游戏的若干拓展及变形 代码如下: #include<cs ...
- HDU 3094 A tree game 树删边游戏
叶节点SG值至0 非叶节点SG值至于它的所有子节点SG值添加1 XOR和后 #include <cstdio> #include <cstring> #include < ...
- HDU 5513 Efficient Tree
HDU 5513 Efficient Tree 题意 给一个\(N \times M(N \le 800, M \le 7)\)矩形. 已知每个点\((i-1, j)\)和\((i,j-1)\)连边的 ...
- HDU 3094 树上删边 NIM变形
基本的树上删边游戏 写过很多遍了 /** @Date : 2017-10-13 18:19:37 * @FileName: HDU 3094 树上删边 NIM变形.cpp * @Platform: W ...
- HDU 4925 Apple Tree(推理)
HDU 4925 Apple Tree 题目链接 题意:给一个m*n矩阵种树,每一个位置能够选择种树或者施肥,假设种上去的位置就不能施肥,假设施肥则能让周围果树产量乘2.问最大收益 思路:推理得到肯定 ...
- HDU 4871 Shortest-path tree 最短路 + 树分治
题意: 输入一个带权的无向连通图 定义以顶点\(u\)为根的最短路生成树为: 树上任何点\(v\)到\(u\)的距离都是原图最短的,如果有多条最短路,取字典序最小的那条. 然后询问生成树上恰好包含\( ...
- Hdu 5379 Mahjong tree (dfs + 组合数)
题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...
- HDU 6035 - Colorful Tree | 2017 Multi-University Training Contest 1
/* HDU 6035 - Colorful Tree [ DFS,分块 ] 题意: n个节点的树,每个节点有一种颜色(1~n),一条路径的权值是这条路上不同的颜色的数量,问所有路径(n*(n-1)/ ...
随机推荐
- 福州大学W班 软件工程课中期调查
问卷地址:https://www.wjx.cn/jq/17054810.aspx
- (转)如何在Eclipse中查看JDK类库的源代码
在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...
- Python 二分查找
(非递归实现) def binary_search(alist, item): first = 0 last = len(alist)-1 while first<=last: midpoint ...
- 国内maven仓库地址 || 某个pom或者jar找不到的解决方法
解决方法 建议在maven仓库中新建settings.xml,然后把如下内容粘贴进去即可.也可以找到maven的安装目录中的conf/settings.xml,把如下的mirrors节复制到对应部分. ...
- .net mvc 利用分部视图局部刷新.
页面利于$.Ajax: $(function(){ $("#btnpartview").click(function () { var model = []; model.push ...
- ThreadLocal源码分析:(一)set(T value)方法
在ThreadLocal的get(),set()的时候都会清除线程ThreadLocalMap里所有key为null的value. 而ThreadLocal的remove()方法会先将Entry中对k ...
- Python模块configparser(操作配置文件ini)
configparser模块提供对ini文件的增删改查方法. ini文件的数据格式: [name1] attribute1=value1 attribute2=value2 [name2] attri ...
- 第三章 JavaScript操作BOM对象
第三章 JavaScript操作BOM对象 一.window对象 浏览器对象模型(BOM)是javascript的组成之一,它提供了独立与浏览器窗口进行交换的对象,使用浏览器对象模型可以实现与HT ...
- Mysql数据库主从配置
一.为什么要使用数据库主从架构 一个网站损耗资源最厉害的就是数据库,最易崩溃的也是数据库,而数据库崩溃带来的后果是非常严重的.数据库分为读和写操作,在实际的应用中,读操作的损耗远比写操作多太多,因此读 ...
- python-map的用法
map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 1.当seq只 ...