Game on Tree
D - Game on Tree
Time limit : 2sec / Memory limit : 256MB
Score : 1100 points
Problem Statement
There is a tree with N vertices numbered 1,2,…,N. The edges of the tree are denoted by (xi,yi).
On this tree, Alice and Bob play a game against each other. Starting from Alice, they alternately perform the following operation:
- Select an existing edge and remove it from the tree, disconnecting it into two separate connected components. Then, remove the component that does not contain Vertex 1.
A player loses the game when he/she is unable to perform the operation. Determine the winner of the game assuming that both players play optimally.
Constraints
- 2≤N≤100000
- 1≤xi,yi≤N
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x1 y1
x2 y2
:
xN−1 yN−1
Output
Print Alice
if Alice wins; print Bob
if Bob wins.
Sample Input 1
5
1 2
2 3
2 4
4 5
Sample Output 1
Alice
If Alice first removes the edge connecting Vertices 1 and 2, the tree becomes a single vertex tree containing only Vertex 1. Since there is no edge anymore, Bob cannot perform the operation and Alice wins.
Sample Input 2
5
1 2
2 3
1 4
4 5
Sample Output 2
Bob
Sample Input 3
6
1 2
2 4
5 1
6 3
3 2
Sample Output 3
Alice
Sample Input 4
7
1 2
3 7
4 6
2 3
2 4
1 5
Sample Output 4
Bob
在树上的博弈,从0,1开始看看这棵树的异或和
#include <bits/stdc++.h>
using namespace std;
const int N=;
vector<int>vec[N];
int dfs(int u,int fa) {
int re=;
for(int i=; i<vec[u].size(); i++) {
if(vec[u][i]!=fa) {
re^=+dfs(vec[u][i],u);
}
}
return re;
}
int main() {
int n;
scanf("%d",&n);
for(int i=; i<n; i++) {
int a,b;
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
}
printf(dfs(,)?"Alice\n":"Bob\n");
return ;
}
Game on Tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Tree树节点选中及取消和指定节点的隐藏
指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...
随机推荐
- /usr/local/sbin/fping -s www.baidu.com www.google.com
/usr/local/sbin/fping -s www.baidu.com www.google.com
- SqlServer 填充因子的说明
CREATE NONCLUSTERED INDEX IX_d_name ON department(d_name) with fillfactor=30 使用 fill factor 选项可以指定 M ...
- jmeter参考网址
http://blog.csdn.net/dongdong9223/article/details/49248979 http://blog.csdn.net/hjh00/article/detail ...
- Linux下如何修改用户默认目录
Linux下默认的用户目录一般为/home/xxx(root用户除外),有些时候我们可能需要修改这个目录,下面我就给大家分享2中修改的方法 工具/原料 Linux操作系统 方法/步骤 1 1.切换 ...
- xcode技巧
1.统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 find . -name "*.m" -or -name "*.h" -or -name ...
- thinkphp 跳转
1 $this -> redirect('index',array('type'=>2,'id'=>0)); //直接跳转 2 $this->success('提交失 ...
- 阻止form元素内的input标签回车提交表单
<form></form>标签内input元素回车会默认提交表单. 阻止回车默认提交表单: $('form').on('keydown', function (event) { ...
- c++文件偏移
#include <iostream> #include <fstream> #include <cassert> using namespace std; int ...
- 服务器上搭建flowvisor平台
之前全是在virtualbox上的Ubuntu虚拟机上测试的ovs以及pox, 现在我们开始在服务器上开始了 两台服务器上的ovs均是1.4.6版本 遇到一个问题:之前装的ovs down了 然后什么 ...
- 【转】Qt Socket简单通信
最近要用到Qt的Socket部分,网上关于这部分的资料都比较复杂,我在这总结一下,把Socket的主要部分提取出来,实现TCP和UDP的简单通信. 1.UDP通信 UDP没有特定的server端和cl ...