Game

Description

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 take turns moving the stones. On each turn, the player
chooses exactly ONE of his stones, moves this stone from current node to its parent node. During the
game, any number of stones can be put on the same node.
The player who moves all of his stones to the root of the tree is the loser. Assume that Bob and
Alice are both clever enough. Given the initial positions of the stones, write a program to find the
winner.

Input Description
Input contains multiple test cases (less than 1500 cases).
The first line of each test case contains three integers: n( 1<n<=10 ), m1 ( 1<=m1<=3 ), m2
( 1<=m2<=3 ), n is the number of nodes.
Next n-1 lines describe the tree. Each line contains two integers A and B in range [0,n),
representing an edge of the tree. Node 0 is the root.
There are m1 integers on next two lines, representing the initial positions of Alice´s and Bob´s
stones.
There is a blank line after each test case.
Output Description
Output the winner´s name on a single line for each test case.
Sample Input
3 1 1
0 1
2 0
1
2
3 2 1
0 1
1 2
2 2
2
Sample Output
Bob
Alice
 
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
bool vis[18];
bool map[18][18];
int dis[18];
int main()
{
	int n,m1,m2;
	while(scanf("%d%d%d",&n,&m1,&m2)==3)
	{
		memset(map,0,sizeof(map));
		memset(vis,0,sizeof(vis));
		int u,v;
		for(int i=1;i<n;i++)
		{
			scanf("%d%d",&u,&v);
			map[u][v]=map[v][u]=1;
		}
		dis[0]=0;
		queue <int> q;
		q.push(0);
		while(!q.empty())
		{
			int p=q.front();
			q.pop();
			for(int i=0;i<n;i++)
			{
				if(map[p][i]&&!vis[i])
				{
					q.push(i);
					vis[i]=1;
					dis[i]=dis[p]+1;
				}
			}
		}
		int sum1=0,sum2=0,weizhi;
		for(int i=1;i<=m1;i++)
		{
			scanf("%d",&weizhi);
			sum1+=dis[weizhi];
		}
		for(int i=1;i<=m2;i++)
		{
			scanf("%d",&weizhi);
			sum2+=dis[weizhi];
		}
		if(sum1>sum2)printf("Alice\n");
		else printf("Bob\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值