jzoj 1350. 游戏 (Standard IO)

Description
  Bob经常与Alice一起玩游戏。今天,他们在一棵树上玩游戏。Alice有M1块石子,Bob有M2块石子,游戏一开始,所有石头放在树的节点处,除了树根。Alice先移然后两人轮流移动,每次移动只能选择自己的一个石子,而且只能从当前位置移到父亲节点处,游戏过程中允许一个节点处放多个石子。
  谁先把自己所有的石子移到树根处谁就失败了,假设两人都是非常聪明,游戏过程中都使用最优策略,给定石子起始位置,要你计算出谁是赢家。

Input
  输入包含多组测试数据。
  第一行输入T(T<=10)表示测试数据组数。
  接下来每组测试数据第一行输入3个整数N(1<=N<=10000),M1(1<=M1<=10000),M2(1<=M2<=10000),其中N表示树的节点数。
  接下来N-1行描述树,每行包含两个整数A和B(0<=A,B<=N-1)表示树中有一条边连接A,B两点,注意0是树根。
  接下来一行M1个数,表示Alice的M1个石子的位置。
  接下来一行M2个数,表示Bob的M2个石子的位置。

Output
  对于每组测试数据,输出赢家的名字。

Sample Input
2
3 1 1
0 1
2 0
1
2
3 2 1
0 1
1 2
2 2
2

Sample Output
Bob
Alice
Data Constraint

Hint
【数据说明】
  30%的数据满足1<=N<=10,1<=M1,M2<=3
  
//written by zzy

题目大意:

A和B玩游戏,每人轮流将一个自己的石头往上移,到根就停,问谁先不能动

题解:

因为移石子的顺序随意,所以答案就是比较每个人的石头的深度和的大小.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 10005
using namespace std;

int p,q;
int i,j,n,m1,m2,l,u,v,ans_A,ans_B;
int s1[N],s2[N],d[N];
bool vis[N];

int next[N*2],list[N],to[N*2];

void add(int x) {
	next[++l]=list[x];
	list[x]=l;
}

void dfs(int x) {
	for (int t=list[x],y=to[t];t;t=next[t],y=to[t]) {
		if (!vis[y]) {
			vis[y]=true; d[y]=d[x]+1; dfs(y);
		}
	}
}

int main()
{
	scanf("%d",&p);
	for (q=1;q<=p;q++) {
	scanf("%d%d%d",&n,&m1,&m2);
	l=0;
	memset(list,0,sizeof(list));
	for (i=1;i<=n-1;i++) { 
	 scanf("%d%d",&u,&v);
	 to[i*2-1]=v; add(u);
	 to[i*2]=u; add(v);
    }
    for (i=1;i<=m1;i++)
     scanf("%d",&s1[i]);
    for (i=1;i<=m2;i++)
     scanf("%d",&s2[i]);   
    memset(vis,false,sizeof(vis));
	vis[0]=true;  
    dfs(0);
    ans_A=ans_B=0;
    for (i=1;i<=m1;i++)
     ans_A+=d[s1[i]];
    for (i=1;i<=m2;i++)
     ans_B+=d[s2[i]];
    if (ans_A>ans_B) printf("Alice\n");
	else printf("Bob\n");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章