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");
}
}
}