CF1405D「Tree Tag」
1. 题目
题目链接:CF1405D「Tree Tag」 。
DESCRIPTION
Alice and Bob are playing a fun game of tree tag.
The game is played on a tree of vertices numbered from to . Recall that a tree on vertices is an undirected, connected graph with edges.
Initially, Alice is located at vertex , and Bob at vertex . They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most from the current vertex. And in a move, Bob can jump to a vertex with distance at most 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 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 (). Description of the test cases follows.
The first line of each test case contains five integers () — the number of vertices, Alice’s vertex, Bob’s vertex, Alice’s maximum jumping distance, and Bob’s maximum jumping distance, respectively.
The following lines describe the edges of the tree. The -th of these lines contains two integers (), denoting an edge between vertices and . It is guaranteed that these edges form a tree structure.
It is guaranteed that the sum of across all test cases does not exceed .
Output
For each test case, output a single line containing the winner of the game: “Alice” or “Bob”.
Example
Input #1
1 | 4 |
Output #2
1 | Alice |
2. 题解
分析
题目描述感觉有点诘屈聱牙,这个问题主要就是说如果在有限步骤内,Alice 和 Bob 能够同时移动到同一个顶点上,那么 Alice 赢;否则 Bob 赢。就相当于 Bob 逃,Alice 追,追得上 Alice 赢,追不上 Bob 赢。这道题蒟蒻的我也是看了官方题解才弄懂的(Orz)。
对于这道题应该分情况讨论:设树中 到 的唯一简单路径长度为 ,树的直径为 ,则
-
若 ,则由于 Alice 先手故第一步 Alice 就追上了 Bob,Alice 赢。
-
若 ,则 Alice 可以先移动到树的中间位置,使得 Alice 下一步可以移动到整棵树的任意一个结点。易知无论 Bob 怎么逃,Alice 移动两步一定可以追上,Alice 赢。
-
若 ,由于此时 ,,故 Alice 移动后 Bob 还有逃的机会,则 Bob 可以如此应对:
- 如果 Alice 下一步可以移动到 Bob 当前结点位置,则 Bob 应该以 db 步长移动到树的直径上的某点,此时由于 故下一步 Alice 追不上 Bob 。
- 如果 Alice 下一步追不上 Bob,则 Bob 只需待在原位即可。
- 若 ,Alice 可以采取如下策略获胜:
- 若下一步无法追上 Bob,则以 为树根, Alice 朝 Bob 所在子树方向前进 1 步。如果 Bob 下一步逃离当前子树,则 Bob 移动的路径一定会经过 ,则 Bob 无法逃离 外大于 的距离(因为当前 )。
- 若下一步能够追上 Bob,则 Alice
直接追上即可。
代码
1 |
|