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.
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;
}