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 ...
随机推荐
- [.net 多线程]Monitor
Monitor 类通过向单个线程授予对象锁来控制对对象的访问.对象锁提供限制访问代码块(通常称为临界区)的能力.当一个线程拥有对象的锁时,其他任何线程都不能获取该锁.还可以使用 Monitor 来确保 ...
- 复制构造函数被调用的三种情况------新标准c++程序设计
1.当用一个对象去初始化同类的另一个对象时,会引发复制构造函数被调用.例如,下面的两条语句都会引发复制构造函数的调用,用以初始化c2. C c2 (c1); C c2=c1; 这两条语句是等价的.注意 ...
- 自用 Pycharm 主题配色分享(主题才是开发第一生产力)
写在前面的话 是的,我又回来了,上一篇[使用 Visual Studio Code(VSCode)搭建简单的 Python + Django 开发环境]才说真香,结果用两天就发现很多恶心的问题拦住了菜 ...
- django中ImageField模块使用
https://blog.csdn.net/meylovezn/article/details/47124923
- 题解 CF500D 【New Year Santa Network】
题目链接 这道题首先是要看看该如何化简,先把三元组化成二元组. 之后统计经过某条边的 次数$*$权值 的和. 最后除以总基数 $tot$ 其中,每条边被计算的次数为 子树的点数$*$非子树的点数 ( ...
- 【python】10分钟教你用python下载和拼接微信好友头像图片
前言 相信微信大家是用得再多也不过了.那么,对于python+微信,又能玩出什么新的花样呢?下面小编就给大家带来一个好玩的东西.用python下载所有的微信好友的头像,然后拼接成一张大图.这样,大家就 ...
- GG and MM HDU - 3595 Every-SG
$ \color{#0066ff}{ 题目描述 }$ 两堆石子,GG和MM轮流取,每次在一堆石子中取另一堆石子的k\((k\ge1)\)倍,不能操作的输 现在二人要玩n个这样的游戏,每回合每个人对每个 ...
- ubutu16.04修改分辨率
http://blog.csdn.net/oiken/article/details/71088230 vmware不能自动适应Ubuntu16.04的分辨率,而且Ubuntu16.04的displa ...
- urlencode编码问题(以及urlparse) 转
网址链接中的中文编码 中文的gbk(GB2312)编码: 一个汉字对应两组%xx,即%xx%xx 中文的UTF-8编码: 一个汉字对应三组%xx,即%xx%xx%xx 可以利用百度进行URL编码解码 ...
- Android newsClient 小实例应用
1.newsClient新闻客户端涉及知识点汇总: (1)ListView(用来显示消息) (2)开子线程去服务器取数据 (3)解析xml文件 (4)利用handler或者runOnUiThread( ...