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; }?
轉載于:https://www.cnblogs.com/chenjianxiang/p/3540892.html
總結
以上是生活随笔為你收集整理的TOJ 4393 Game的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: parameter乱码提交的问题
- 下一篇: xenserver PXE安装系统错误的