描述

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的更多相关文章

  1. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  2. 【HDOJ】4393 Throw nails

    水题,优先级队列. /* 4393 */ #include <iostream> #include <sstream> #include <string> #inc ...

  3. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

  4. TOJ 1139.Compromise

    2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...

  5. 优先队列运用 TOJ 4123 Job Scheduling

    链接:http://acm.tju.edu.cn/toj/showp4123.html 4123.   Job Scheduling Time Limit: 1.0 Seconds   Memory ...

  6. 最小生成树 TOJ 4117 Happy tree friends

    链接http://acm.tju.edu.cn/toj/showp4117.html 4117.   Happy tree friends Time Limit: 1.0 Seconds   Memo ...

  7. TOJ 4120 Zombies VS Plants

    链接:http://acm.tju.edu.cn/toj/showp4120.html 4120.   Zombies VS Plants Time Limit: 1.0 Seconds   Memo ...

  8. TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量

    It's not Floyd Algorithm 时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte   描述 When a directed grap ...

  9. TOJ 4523 Transportation

    Description Given N stations, you want to carry goods from station 1 to station N. Among these stati ...

随机推荐

  1. Math对象的使用

    1. Math.floor() === 向下取整 2.Math.random() === 取一个浮点随机数 3.Math.round() === 四舍五入后一个最接近的整数 4.Math.ceil() ...

  2. 2张图简单分析count(0)与count(*)

    以前一直以为count(0)查询效率比count(*)比较高,原因大概是这么认为count(0)只是第一列进行统计,而count(*)所有列放在一起统计(亲,不要误会,这里不是所有列累加哦) 结果真的 ...

  3. 关于CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary AS

    1.添加用户"Network Service” 和 “IIS_IUSERS” 读下面目录的读写权限 a) C:\Windows\Temp b) C:\Windows\Microsoft.NE ...

  4. [Algorithm]排序

    一.排序算法 1.插入排序 1) 直接插入排序:(插入类) 1 void InsertSort( ElemType R[], int n ) 2 { 3 for ( int i = 2; i < ...

  5. Mybatis中的多表查询 多对一,一对多

    示例:用户和账户 一个用户可以有多个账户 一个账户只能属于一个用户(多个账户也可以属于同一个用户) 步骤: 1.建立两张表:用户表,账户表 让用户表和账户表之间具备一对多的关系:需要使用外键在账户表中 ...

  6. java 实验2 类

    共5道大题  最后一题为自动洗牌发牌系统 1) 编写一个类实现银行帐户的概念.包括的属性有:帐号.储户姓名.地址.存款余额,包括的方法有:存款.取款.查询.计算利息.累加利息等. public cla ...

  7. mybatis 学习笔记(四):mybatis 和 spring 的整合

    mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...

  8. Java网络编程客户端和服务器通信

    在java网络编程中,客户端和服务器的通信例子: 先来服务器监听的代码 package com.server; import java.io.IOException; import java.io.O ...

  9. [转]Why you shouldn't use set (and what you should use instead)

    Why you shouldn't use set (and what you should use instead) --- stl::set和sorted ector对比Matt Austern ...

  10. JDBC_时间处理_Date_Time_Timestamp区别_随机日期生成

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement;import ...