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 ;
}
最新文章
- Git的冲突解决过程
- Web开发中管理ipad屏幕的方向变化
- css 导航,菜单对应页面切换效果实现方法
- synchronized锁自旋2
- 海康威视摄像头SDK-网页版(NetVideoActiveX23.cab安装)
- [hackerrank]Service Lane
- [iOS基础控件 - 6.12.3] @property属性 strong weak copy
- CoordinatorLayout的简单应用(材料设计新控件)
- win7安装gevent时报错 whl is not a supported wheel on this platform.
- Hadoop2.2编程:新旧API的区别
- jQuery 源码细读 -- $.Callbacks
- Android学习----AndroidManifest.xml文件解析
- Python学习日志(六)
- zoj1610(线段树)
- vue关于数组使用的坑
- 《前端之路》之 this 的使用技巧总结
- Java【第六篇】面向对象基础
- 为github公开项目单独设置用户名
- 个人作业 - Week3 - 案例分析
- IO、NIO、AIO
热门文章
- react中constructor和super()以及super(props)的区别。
- linux机器上部署多台Tomcat
- Memcached笔记之分布式算法
- js判断是否为app
- DROP LANGUAGE - 删除一个过程语言
- Mycat高可用解决方案二(主从复制)
- centos7.4进入单用户模式
- destoon公司账户增加销售区域等下拉列表配置
- paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之fsm1各种style的timing/area比较
- Thinkphp5 获取执行的sql语句