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 ...
随机推荐
- Nginx服务器301跳转到带www的域名的方法
为什么要这么做? 我们的域名在做解析时经常会解析2个域名,即带www的和不带www的.这样做的目的是,当用户使用不带www的域名时,也可以正常访问你的网站.但是这样做的后果是,你站点主域名的PR值分散 ...
- 苹果ATS Win2008 R2 IIS7.5 HTTPS 证书的那些可能遇到的坑
前言:工作这么多年,每一次要弄https 都和苹果有关,上一次是苹果app的企业安装形式,ios7后 .plist 文件必须在一个https路径. 这一次则是苹果的ATS计划,无疑这是在推动网络安全上 ...
- .vue公共组件裁减导航
场景: 有一个公共头部和底部,vue搭建的框架,在app.vue里写的公共方法,首页是个登录页面,不需要公共部分,在这基础上进行公共部分的显示隐藏. 即注册页.登录页.404页面都不要导航 代码: ( ...
- 使用Python生成ASCII字符画
使用Python生成ASCII字符画 在很多的网站主页中或者程序的注释中会有一些好看的字符注释画.显得很牛逼的样子 例如: 知乎 _____ _____ _____ _____ /\ \ /\ \ / ...
- IntelliJ IDEA java设置程序运行时内存
Run/Edit Configurations Configuration/VM options 例如:设置运行内存为:-Xmx3m -Xms3m
- oracle 数据导到 sql server
方法一: navicate:用法比较简单,选择工具-数据传输就可以了.目前测试了下暂时没遇到什么问题. 方法二: Microsoft SQL Server Migration Assistant 8. ...
- python 搜集参数
def print_params(*params): print(params) print_params('Testing')print_params(1,2,3) #参数前的星号将所有值放置在同一 ...
- fei33423 工作 职场 格言
对老板: 1. 老板不知道你做的事情(目标设定) 2. 老板要的是规划(对上报告), 自己给自己设定 金字塔四位下的目标,各种维度.如何细化. 2.1 明确老板期望 2.2 与老板达成共识 2.3 ...
- runtime实践之Method Swizzling
利用 Objective-C 的 Runtime 特性,我们可以给语言做扩展,帮助解决项目开发中的一些设计和技术问题.这一篇,我们来探索一些利用 Objective-C Runtime 的黑色技巧.这 ...
- 【点分治】luoguP2664 树上游戏
应该是一道中等难度的点分?麻烦在一些细节. 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] ...