Tree Tag (Codeforces 1404B)
Tags: DP, games

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 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains five integers $$$n,a,b,da,db$$$ ($$$2\le n\le 10^5$$$, $$$1\le a,b\le n$$$, $$$a\ne b$$$, $$$1\le da,db\le 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\le u, v\le n, u\ne 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".

Note: In the first test case, Alice can win by moving to vertex $$$1$$$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move.  In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $$$1$$$ or $$$6$$$ is farthest from Alice.  

Sample Inputs:
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

Sample Outputs:
Alice
Bob
Alice
Alice

Solution (Python):
import sys

def minp():
    return sys.stdin.readline().strip()

def mint():
    return int(minp())

def mints():
    return map(int, minp().split())

def solve():
    (n, a, b, da, db) = mints()
    e = [[] for i in range(n + 1)]
    for i in range(n - 1):
        (u, v) = mints()
        e[u].append(v)
        e[v].append(u)
    if db < da * 2 + 1:
        return False
    q = [0] * (n + 2)
    ql = 0
    qr = 1
    d = [None] * (n + 1)
    d[a] = 0
    q[0] = a
    while ql < qr:
        x = q[ql]
        ql += 1
        for v in e[x]:
            if d[v] is None:
                d[v] = d[x] + 1
                q[qr] = v
                qr += 1
    if d[b] <= da:
        return False
    far = q[qr - 1]
    if d[far] >= da * 2 + 1:
        return True
    ql = 0
    qr = 1
    d = [None] * (n + 1)
    d[far] = 0
    q[0] = far
    while ql < qr:
        x = q[ql]
        ql += 1
        for v in e[x]:
            if d[v] is None:
                d[v] = d[x] + 1
                q[qr] = v
                qr += 1
    far2 = q[qr - 1]
    return d[far2] >= da * 2 + 1
for i in range(mint()):
    print(['Alice', 'Bob'][solve()])

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
t = int(input())
for _ in range(t):
    (n, a, b, da, db) = map(int, input().split())
    a -= 1
    b -= 1
    adj = [[] for i in range(n)]
    for _ in range(n - 1):
        (u, v) = map(int, input().split())
        adj[u - 1].append(v - 1)
        adj[v - 1].append(u - 1)
    if db <= 2 * da:
        print('Alice')
    else:
        works = True
        dist = [-1] * n
        q = deque()
        q.append(a)
        dist[a] = 0
        while q:
            v = q.popleft()
            for u in adj[v]:
                if dist[u] == -1:
                    dist[u] = dist[v] + 1
                    q.append(u)
        if dist[b] <= da:
            works = False
        last = v
        dist = [-1] * n
        q = deque()
        q.append(last)
        dist[last] = 0
        while q:
            v = q.popleft()
            for u in adj[v]:
                if dist[u] == -1:
                    dist[u] = dist[v] + 1
                    q.append(u)
        lorg = max(dist)
        if lorg >= 2 * da + 1 and works:
            print('Bob')
        else:
            print('Alice')

Solution (Python):
from heapq import heappush, heappop
from collections import defaultdict, Counter, deque
import threading
import sys
import bisect
input = sys.stdin.readline

def ri():
    return int(input())

def rs():
    return input()

def rl():
    return list(map(int, input().split()))

def rls():
    return list(input().split())
threading.stack_size(10 ** 8)
sys.setrecursionlimit(10 ** 6)

def main():
    for _ in range(ri()):
        (n, a, b, da, db) = rl()
        g = defaultdict(list)
        for e in range(n - 1):
            (u, v) = rl()
            g[u].append(v)
            g[v].append(u)
        dis = [0] * (n + 1)
        dia = [0]
        dis[a] = 0

        def dfs(cn, p):
            mx = 0
            for nn in g[cn]:
                if nn != p:
                    dis[nn] = dis[cn] + 1
                    cur = 1 + dfs(nn, cn)
                    dia[0] = max(dia[0], cur + mx)
                    mx = max(mx, cur)
            return mx
        dfs(a, -1)
        if dis[b] <= da:
            pos = 1
        elif 2 * da >= dia[0]:
            pos = 1
        elif db > 2 * da:
            pos = 0
        elif db <= 2 * da:
            pos = 1
        print(['Bob', 'Alice'][pos])
    pass
threading.Thread(target=main).start()