Bob经常与Alice一起玩游戏。今天,他们在一棵树上玩游戏。Alice有M1块石子,Bob有M2块石子,游戏一开始,所有石头放在树的节点处,除了树根。Alice先移然后两人轮流移动,每次移动只能选择自己的一个石子,而且只能从当前位置移到父亲节点处,游戏过程中允许一个节点处放多个石子。
谁先把自己所有的石子移到树根处谁就失败了,假设两人都是非常聪明,游戏过程中都使用最优策略,给定石子起始位置,要你计算出谁是赢家。
输入包含多组测试数据。
第一行输入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个石子的位置。
对于每组测试数据,输出赢家的名字。
2
3 1 1
0 1
2 0
1
2
3 2 1
0 1
1 2
2 2
2
Bob
Alice
【数据说明】
30%的数据满足1<=N<=10,1<=M1,M2<=3
我们把整个图存起来,然后用dfs求每个点离根的距离,统计起来看谁大就好了
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
bool B[100005];
int n, m1, m2, T, t, ans1, ans2;
int farf[100005], h[100005];
struct tree
{
int to, next;
}Tree[500005];
void dfs(int x, int dep)
{
B[x] = 1;
farf[x] = dep;
for (int i = h[x]; i; i = Tree[i].next)
if (!B[Tree[i].to]) dfs(Tree[i].to, dep + 1);
}
int main()
{
scanf("%d", &T);
while (T--)
{
memset(B, 0, sizeof(B));
memset(Tree, 0, sizeof(Tree));
memset(farf, 0, sizeof(farf));
ans1 = ans2 = 0;
scanf("%d%d%d", &n, &m1, &m2);
for (int i = 1; i < n; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
Tree[++t] = (tree){y, h[x]}; h[x] = t;
Tree[++t] = (tree){x, h[y]}; h[y] = t;//存图
}
dfs(0, 0);
int x;
for (int i = 1; i <= m1; ++i)
{
scanf("%d", &x);
ans1 += farf[x];
}
for (int j = 1; j <= m2; ++j)
{
scanf("%d", &x);
ans2 += farf[x];
}
if (ans1 == ans2) printf("Bob\n");//因为是Alice先动,如果两者深度一样,先动者输
else {
if (ans1 < ans2) printf("Bob\n");
else printf("Alice\n");
}
}
}