Game-图的基本运用

来源:互联网 发布:怎么导全站仪数据到u盘 编辑:程序博客网 时间:2024/05/22 13:17

Problem 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 first 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

Input contains multiple test cases.
The first line of each test case contains three integers(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 and A is B's parent. Node 0 is the root.
There are m1 integers and m2 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

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

//题意是alicebob玩游戏,alicem1个石子,bobm2个石子,现在需要把各自的石子全部移动到根节点,alice先移,谁不能移动谁就输了。

//邻接矩阵存储,深搜查找各个节点到根节点的路径,并累加,谁多谁赢。

 

#include<cstdio>#include<cstring>int parent[30][30],vis[30],n,m1,m2,maxn;void dfs(int k,int step){    if(k==0)    {        if(step<maxn)        {            maxn=step;            return;        }    }    for(int j=0;j<n;j++)    {        if(!vis[j]&&parent[k][j]==1)        {            vis[j]=1;            dfs(j,step+1);            vis[j]=0;        }    }}int main(){    int a,b,i,alice,bob;    while(scanf("%d%d%d",&n,&m1,&m2)!=EOF)    {        memset(parent,0,sizeof(parent));        for(i=1;i<n;i++)        {            scanf("%d%d",&a,&b);            parent[a][b]=1;            parent[b][a]=1;        }        alice=bob=0;        for(i=1;i<=m1;i++)        {            scanf("%d",&a);            maxn=0xffff;            memset(vis,0,sizeof(vis));            vis[a]=1;            dfs(a,0);            alice+=maxn;        }        for(i=1;i<=m2;i++)        {            scanf("%d",&a);            maxn=0xffff;            memset(vis,0,sizeof(vis));            vis[a]=1;            dfs(a,0);            bob+=maxn;        }        if(alice<=bob)            printf("Bob\n");        else printf("Alice\n");    }    return 0;}


 

 

0 0
原创粉丝点击