CF1405D「Tree Tag」 1. 題目 2. 題解

1. 題目

題目鏈接:CF1405D「Tree Tag」

DESCRIPTION

Alice and Bob are playing a fun game of tree tag.

The game is played on a tree of n vertices numbered from 1 to n. Recall that a tree on n vertices is an undirected, connected graph with n−1 edges.

Initially, Alice is located at vertex a, and Bob at vertex b. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most da from the current vertex. And in a move, Bob can jump to a vertex with distance at most db from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them.

If after at most 10^{100} moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins.

Determine the winner if both players play optimally.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10^4). Description of the test cases follows.

The first line of each test case contains five integers n,a,b,da,db (2≤n≤10^5, 1≤a,b≤n, a≠b, 1≤da,db≤n−1) — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively.

The following n−1 lines describe the edges of the tree. The i-th of these lines contains two integers u, v (1≤u,v≤n,u≠v), denoting an edge between vertices u and v. It is guaranteed that these edges form a tree structure.

It is guaranteed that the sum of n across all test cases does not exceed 10^5.

Output

For each test case, output a single line containing the winner of the game: "Alice" or "Bob".

Example

Input #1
4
4 3 2 1 2
1 2
1 3
1 4
6 6 1 2 5
1 2
6 5
2 3
3 4
4 5
9 3 9 2 5
1 2
1 6
1 9
1 3
9 5
7 9
4 8
4 3
11 8 11 3 3
1 2
11 9
4 9
6 5
2 10
3 2
5 9
8 3
7 4
7 10
Output #2
Alice
Bob
Alice
Alice

2. 題解

分析

題目描述感覺有點詰屈聱牙,這個問題主要就是說如果在有限步驟內,Alice 和 Bob 能夠同時移動到同一個頂點上,那麼 Alice 贏;否則 Bob 贏。就相當於 Bob 逃,Alice 追,追得上 Alice 贏,追不上 Bob 贏。這道題蒟蒻的我也是看了官方題解才弄懂的(Orz)。

對於這道題應該分情況討論:設樹中 ab 的唯一簡單路徑長度爲 (a,b),樹的直徑爲 diameter,則

  • (a,b) \le da,則由於 Alice 先手故第一步 Alice 就追上了 Bob,Alice 贏。

  • diameter \le 2 \times da,則 Alice 可以先移動到樹的中間位置,使得 Alice 下一步可以移動到整棵樹的任意一個結點。易知無論 Bob 怎麼逃,Alice 移動兩步一定可以追上,Alice 贏。

  • 2 \times da \lt db,由於此時 (a,b) > dadiameter \gt 2 \times da,故 Alice 移動後 Bob 還有逃的機會,則 Bob 可以如此應對:

  1. 如果 Alice 下一步可以移動到 Bob 當前結點位置,則 Bob 應該以 db 步長移動到樹的直徑上的某點,此時由於 2 \times da \lt db 故下一步 Alice 追不上 Bob 。
  2. 如果 Alice 下一步追不上 Bob,則 Bob 只需待在原位即可。
  • 2 \times da \ge db,Alice 可以採取如下策略獲勝:
  1. 若下一步無法追上 Bob,則以 a 爲樹根, Alice 朝 Bob 所在子樹方向前進 1 步。如果 Bob 下一步逃離當前子樹,則 Bob 移動的路徑一定會經過 a,則 Bob 無法逃離 a 外大於 da 的距離(因爲當前 (a,b) \ge da)。
  2. 若下一步能夠追上 Bob,則 Alice
    直接追上即可。

代碼

#include <bits/stdc++.h>
#define ll int
#define MAXN 100005
using namespace std;

// 前向星存邊
ll cnt;
ll head[MAXN];
struct edge{
    ll to, next;
}e[2*MAXN];
void init() {
    cnt = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(ll u, ll v) {
    e[cnt].next = head[u];
    e[cnt].to = v;
    head[u] = cnt++;
    e[cnt].next = head[v];
    e[cnt].to = u;
    head[v] = cnt++;
}

ll dia;
ll vis[MAXN];
ll depth[MAXN];
vector <ll> v[MAXN];
// dfs 求出樹的直徑
// 同時求出 a 和 b 的距離
ll dfs(ll a, ll b) {
    vis[a] = 1;
    ll res = 0;
    v[a].clear();
    for(ll i = head[a]; i != -1; i = e[i].next) {
        ll u = e[i].to;
        if(!vis[u]) {
            depth[u] = depth[a] + 1;
            res = dfs(u, b);
            v[a].push_back(res+1);
        }
    }
    if(v[a].size()) {
        sort(v[a].begin(),v[a].end(), greater<ll>());
        if(v[a].size() > 1) {
            dia = max(dia, v[a][0]+v[a][1]);
        } else {
            dia = max(dia, v[a][0]);
        }
        res = v[a][0];
    }
    return res;
}


int main()
{
    ll t;
    scanf("%d", &t);
    for(ll i = 0; i < t; ++i) {
        ll n, a, b, da, db;
        init();
        scanf("%d%d%d%d%d", &n, &a, &b, &da, &db);
        for(ll j = 0; j < n-1; ++j) {
            ll u, v;
            scanf("%d%d", &u, &v);
            addEdge(u, v);
        }
        bool mark;
        dia = 0;
        memset(vis, 0, sizeof(vis));
        memset(depth, 0, sizeof(depth));
        dfs(a, b);
        ll dist = depth[b];
        if(dist <= da) {
            mark = true;
        } else if(dia <= 2*da) {
            mark = true;
        } else if(2*da < db) {
            mark = false;
        } else {
            mark = true;
        }
        printf("%s\n", mark ? "Alice" : "Bob");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章