TOJ 4393 Game
描述
Bob always plays game with Alice.Today,they are playing a game on a tree.Alice has m1 stones,Bob has m2 stones.At the beginning of the game,all the stones are placed on the nodes of a tree,except the root.Alice moves first and they turns moving the stones.On each turn,the player chooses exactly one of his stone,moves this stone from current node to his parent node.During the game,any number of stone can be put on the same node.
the player who first moves all of his stones to the root of the tree is
the loser.Assurme that Bob and Alice are both clever enough.Given the
initial position of the stones,write a program to find the winner.
输入
Input contains multiple test case.
The first line of each test case cotains three integers
n(1<n<=10),m1(1<=m1<=3) and m2(1<=m2<=3) ,n is the
number of node.
Next n-1 line describe the tree.Each line contains two integers A and B
in range [0,n) representing an edge of the tree and A is B's
parent.Node 0 is root.
There are m1 integers and m2 integers on the next two lines,representing the initial position of Alice's and Bob's stones.
输出
Output the winner of the game.
样例输入
3 1 1
0 1
0 2
1
2
3 2 1
0 1
1 2
2 2
2
样例输出
Bob
Alice
看了半天没看懂题目是什么意思,后来看了一个人的一篇博客才明白是大概是什么的意思。
应该是先把所有的stones移到根节点的人算输,这样子的话只要是把所有的stones所在层数相加和最小的人就输了。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define MAXN 20
using namespace std; int n,m1,m2,cnt;
int dist[MAXN];
int visited[MAXN];
int head[MAXN]; struct Edge{
int to,next;
}edge[MAXN*]; void addedge(int u, int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void bfs(){
queue<int> Q;
Q.push();
dist[]=;
while(!Q.empty()){
int t=Q.front();
Q.pop();
for(int i=head[t]; i!=-; i=edge[i].next){
int c=edge[i].to;
if(!visited[c]){
visited[c]=;
dist[c]=dist[t]+;
Q.push(c);
}
}
}
}
int main(int argc, char *argv[])
{
int u,v,p1,p2;
while(scanf("%d %d %d",&n,&m1,&m2)!=EOF){
cnt=;
memset(head,-,sizeof(head));
memset(visited,,sizeof(visited));
memset(dist,,sizeof(dist));
for(int i=; i<n; i++){
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
}
int sum1=,sum2=;
dist[]=;
bfs();
for(int i=; i<m1;i++){
scanf("%d",&p1);
sum1+=dist[p1];
}
for(int i=; i<m2; i++){
scanf("%d",&p2);
sum2+=dist[p2];
}
if(sum1>sum2){
puts("Alice");
}else{
puts("Bob");
}
}
return ;
}
TOJ 4393 Game的更多相关文章
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- 【HDOJ】4393 Throw nails
水题,优先级队列. /* 4393 */ #include <iostream> #include <sstream> #include <string> #inc ...
- TOJ 1702.A Knight's Journey
2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...
- TOJ 1139.Compromise
2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...
- 优先队列运用 TOJ 4123 Job Scheduling
链接:http://acm.tju.edu.cn/toj/showp4123.html 4123. Job Scheduling Time Limit: 1.0 Seconds Memory ...
- 最小生成树 TOJ 4117 Happy tree friends
链接http://acm.tju.edu.cn/toj/showp4117.html 4117. Happy tree friends Time Limit: 1.0 Seconds Memo ...
- TOJ 4120 Zombies VS Plants
链接:http://acm.tju.edu.cn/toj/showp4120.html 4120. Zombies VS Plants Time Limit: 1.0 Seconds Memo ...
- TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量
It's not Floyd Algorithm 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 描述 When a directed grap ...
- TOJ 4523 Transportation
Description Given N stations, you want to carry goods from station 1 to station N. Among these stati ...
随机推荐
- Func和Action的介绍及其用法
Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如在反射中使用就可以弥补反射所损失的性能. ...
- 深入理解python中可迭代对象,迭代器,生成器
英文原文出处:Iterables vs. Iterators vs. Generators 在python学习中,通常会陷入对以下几个相关概念之间的确切差异的困惑中: a container(容器) ...
- c#读取数据库内容
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- ubuntu 下安装ffmpeg
FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视 频的完整解决方案.它包含了非常先进的音频/视频编解码库 ...
- angular 新建组件
创建组件 ng g component componentName 第一步:引入其他组件 ngFor指令与数据绑定(数据驱动视图) 父组件穿值到自组建
- 自建脚手架之配置中心--LightConf的实现
常规项目开发过程中, 通常会将配置信息位于在项目resource目录下的properties文件文件中, 配置信息通常包括有: jdbc地址配置.redis地址配置.活动开关--等等.因此每次上线或者 ...
- 6w4:第六周程序填空题1
描述 下面程序的输出结果是: A::Fun C::Do 请填空: #include <iostream> using namespace std; class A { private: i ...
- 使用django进行发送 邮件
我们来看一下 django发送 邮件的整个流程 第一步:例先去 网易163注册账号并激活发邮件功能 把授权码进行 开启 来到我们的项目setting中进行 一个配置: # 邮箱的配置信息 EMAIL_ ...
- ubuntu下安装谷歌浏览器
deb 是 Debian Linux 的安装格式,在 ubuntu 中同样可以使用.要安装 deb 安装包,需要使用 dpkg这个终端命令,命令格式如下: $ sudo dpkg -i <pac ...
- npm 修改源 pip 修改源
npm修改源由于不可说原因,npm install时,速度总是不尽如人意,解决办法是修改npm的数据源npm config set registry https://registry.npm.taob ...