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*2];

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(0);
    dist[0]=0;
    while(!Q.empty()){
        int t=Q.front();
        Q.pop();
        for(int i=head[t]; i!=-1; i=edge[i].next){
            int c=edge[i].to;
            if(!visited[c]){
                visited[c]=1;
                dist[c]=dist[t]+1;
                Q.push(c);
            }
        } 
    }
}
int main(int argc, char *argv[])
{
    int u,v,p1,p2;
    while(scanf("%d %d %d",&n,&m1,&m2)!=EOF){
        cnt=0;
        memset(head,-1,sizeof(head));
        memset(visited,0,sizeof(visited));
        memset(dist,0,sizeof(dist));
        for(int i=1; i<n; i++){
            scanf("%d %d",&u,&v);    
            addedge(u,v);
            addedge(v,u);
        }
        int sum1=0,sum2=0;
        dist[0]=0;
        bfs();
        for(int i=0; i<m1;i++){
            scanf("%d",&p1);
            sum1+=dist[p1];
        }
        for(int i=0; i<m2; i++){
            scanf("%d",&p2);
            sum2+=dist[p2];
        }
        if(sum1>sum2){
            puts("Alice");
        }else{
            puts("Bob");
        }
    }
    return 0;
}

 

你可能感兴趣的:(game)