We are given a tree with N vertices. Takahashi at u, Aoki at v. They alternate moves: first Takahashi moves (if not same vertex), then check; then Aoki moves (if not same), then check; repeat. Takahashi wants to maximize the number of moves (or time) before the game ends. Aoki wants to minimize it. Both know each other's position and optimal strategies. It is a game of tag where T is trying to run away, A is trying to catch. Actually typical interpretation: Takahashi is the pursuer? Wait: The game description: "Takahashi is standing at Vertex u, and Aoki is standing at Vertex v." Then they play tag as follows: 1. If same vertex, end. Else Takahashi moves to adjacent vertex. 2. If same vertex, end. Else Aoki moves to adjacent vertex. 3. Go to 1. So Takahashi moves first. Usually in tag, the one moving first is the tagger? But here they say "Takahashi is standing at Vertex u, and Aoki is standing at Vertex v." Not specified who is the tagger. But from the moves: they alternate. They both move. The game ends when they are at the same vertex. So it's like both moving, and they want to meet? But note: Usually tag is one chases the other. However here both move, but one wants to delay meeting, the other wants to hasten meeting. So effectively Takahashi is the runner (trying to avoid being caught), Aoki is the chaser (trying to catch). Because Takahashi moves first, so he has a slight advantage.
Thus we have a tree. The game is a pursuit-evasion with alternating moves, both players moving one step per turn. This is a classic problem: The evader tries to maximize capture time, the pursuer tries to minimize. Both optimal play. On a tree, the optimal strategy for the pursuer is to move along the unique simple path towards the evader? But since the evader moves as well, the dynamics are like a game. But we need to compute the number of moves Aoki will perform before the game ends. Note that moves count: The game proceeds in rounds: Takahashi's move then Aoki's move is one full round, but the problem asks for the number of moves Aoki will perform before the end. That's the number of times Aoki moves. Since Aoki moves after Takahashi, and the game may end after Takahashi's move (if he moves onto Aoki's vertex) or after Aoki's move. The count of Aoki's moves until the game ends.
We need to compute under optimal play from both sides.
We have a tree. Starting positions u (evader), v (pursuer). Takahashi moves first. He can choose any adjacent vertex. Aoki then moves to any adjacent vertex. Both know each other's positions.
We need to find the outcome under optimal play: the number of moves Aoki makes.
Interpretation: This is a two-player game with perfect information and deterministic moves. It's a zero-sum game where payoff is the number of Aoki moves (or capture time). Takahashi wants to maximize it, Aoki wants to minimize.
We need to compute that value.
Given tree up to 1e5 nodes. We need efficient algorithm.
Let's reason about the game.
We can model positions (states) as (t_pos, a_pos, turn). Turn indicates whose move is next. But it's deterministic alternating: after each move, turn flips. Starting state: before any moves, at turn = Takahashi's move? Actually game starts with step 1: If same vertex? Initially they might be different; then Takahashi moves. So initial turn is Takahashi's move. After Takahashi moves, we check if same; if not, Aoki moves. So the state after Aoki's move, we go back to Takahashi. So the cycle continues. The game ends when positions coincide. The number of Aoki moves performed is the count of Aoki moves done until capture. Could capture happen on Takahashi's move? Possibly if Takahashi moves onto Aoki's vertex. Then Aoki wouldn't move that round. So number of Aoki moves could be less than rounds.
We need to compute optimal play outcome.
This is reminiscent of "cops and robbers" on graphs but with alternating moves and both moving exactly one step per turn. In the classic pursuit-evasion on a tree, the cop (pursuer) can always catch the robber (evader) in at most the tree's diameter? Actually on a tree, if both move alternately and cop moves first? There is a known result: the cop number of a tree is 1 (one cop can catch robber). But in the classic game of cops and robbers, the robber moves first, then cop moves; both can move to adjacent vertices. The cop wins if they occupy same vertex at any time. On a tree, the cop has a winning strategy: go towards the robber along the unique path; the distance decreases by 1 each cop move? Actually because robber also moves, but the cop can guarantee capture in at most the length of the longest path? But there is also the concept of the tree's "center" and "radius". In the classic game, the cop can always capture the robber in at most the radius of the tree? Hmm.
But here, the pursuer (Aoki) moves second in each round? Actually order: T moves first, then A moves. So each round: T moves, then A moves. In the classic cops and robbers, usually robber moves first, then cop moves. So it's similar: robber (Takahashi) moves first, cop (Aoki) moves second. So it's exactly that standard formulation. And it's known that on a tree, the cop can catch the robber in at most the length of the longest path from the starting position? But there is a known result: If the cop starts at a vertex that is at distance d from the robber, the capture time can be something like floor(d/2)? No.
Let's consider the game. We have two players on a tree. Both know each other's positions. The robber wants to avoid capture as long as possible. The cop wants to capture as soon as possible. The robber moves first. Each turn, each moves exactly one step along an edge.
This is a classic game often studied as "cops and robbers" or "pursuit-evasion". On a tree, the cop always wins, and the optimal capture time can be computed as the length of the unique path between them? Actually think: Suppose they are at distance D (number of edges) initially. If they were both moving on a line, and robber moves first away from cop, what happens? Consider a simple path (line). Positions: rob at x, cop at y, with x<y (say). Robber can move left or right. Usually, the optimal for robber is to move away from the cop, increasing distance. The cop tries to close distance. But because robber moves first, the distance may not decrease as fast. Let's simulate a line: vertices 1..N. Let rob at u, cop at v. Distance d = |u-v|. If rob moves first, he can move away from cop (increase d by 1). Then cop moves towards robber, decreasing distance by 1. Net effect: distance remains same? Actually after rob moves away, new distance = d+1 if he moved away; cop then moves towards robber along the path, reducing distance by 1 => d again. So distance stays constant if both always move optimally? But if the robber reaches an endpoint (leaf), he cannot increase distance; he must move back towards cop if cop is not at that leaf? Actually at leaf, the only move is back towards cop, decreasing distance. So capture time depends on distance to leaves.
In a tree, the robber's best strategy is to initially move towards a leaf far away, trying to maximize the distance he can keep away from the cop. The cop's best strategy is to move towards the robber along the unique path. The outcome essentially is determined by distances from the cop's start to various leaves relative to the robber's start.
We can reframe: Since both play optimally, the game can be thought of as: The robber (Takahashi) will try to get as far away as possible, but the cop (Aoki) chases. After each full round (both moved), the distance between them either remains same (if robber moves away from cop along the path cop is on) or changes based on branching.
Let's denote positions after each full round? But we need count of Aoki's moves until capture.
We can consider a known result: In the game of cops and robbers on a tree where cop moves after robber, the capture time is equal to the maximum distance from the robber's starting vertex to any leaf, but only if the cop starts at some vertex? Not exactly.
Maybe we can solve by modeling the game as: The robber wants to maximize the number of rounds before capture, and the cop wants to minimize. At each step, the robber moves, then the cop moves. This is a finite game, we can compute outcome via DP on tree? But N up to 1e5, and state space is huge (N^2). However due to tree structure and monotonicity, we can find a simpler expression.
Let's try to analyze more systematically.
We have tree T. Positions: r (robber/Takahashi) and c (cop/Aoki). Turn: robber to move. The game ends when r=c.
Define f(r,c) = number of cop moves before capture, assuming both play optimally with robber to move. Actually careful: The number of cop moves until capture, starting with robber at r, cop at c, and it's robber's turn to move (since initially it's Takahashi's move). That's what we want: f(u,v). We need to compute f.
Note that if r=c initially, then game ends with 0 cop moves.
Otherwise, robber moves. He chooses an adjacent vertex r' (neighbor of r). Then if r'=c, capture occurs immediately (after robber's move, before cop moves). In that case cop moves count = 0 (since capture before cop's move). But wait: The problem says: In step 1: If Takahashi and Aoki are at same vertex, game ends. Else Takahashi moves to adjacent vertex. Then step 2: If they are at same vertex, game ends. So if after Takahashi's move they are same, game ends without Aoki moving. So indeed if robber moves onto cop, number of Aoki moves = 0 for that round.
Otherwise, after robber moves to r' ≠ c, then it's cop's turn. Cop chooses an adjacent vertex c' (neighbor of c). If c' = r', capture occurs after cop's move, so that cop move counts as 1 (the Aoki move) and then game ends. Else, new state (r',c') with robber to move again.
Thus we have recurrence:
f(r,c) =
If r=c: 0
Else:
Robber chooses r' ∈ N(r) to maximize some value? After robber moves, if r'=c then capture before cop's move -> number of cop moves = 0 (since capture after robber's move, before cop moves). So in that branch, the resulting value (number of future cop moves) is 0. Otherwise, after robber moves to r'≠c, cop then moves: cop chooses c' ∈ N(c) to minimize something: the game then continues from (r',c') with robber to move. After cop moves, if c' = r' capture happens, then the count of cop moves is 1 (the just made move) plus future? Actually capture immediate, so total cop moves = 1. But careful: In the recursion, f(r',c') defined as number of cop moves from state (r',c') with robber to move. But if after cop move we have state (r',c') but c' may equal r'? Actually if cop moves to r', then they are same, capture occurs, so no further moves. So the total cop moves from the original state if robber moves to r' and cop moves to c' = (if c'=r' then 1 else 1 + f(r',c')). So from the robber's perspective, after choosing r', the cop will choose c' to minimize the resulting total cop moves. So the value after robber chooses r' is:
val(r') =
if r' = c: 0
else: min_{c' ∈ N(c)} [ (1 if c'=r' else 1 + f(r',c')) ]
but note: cop may also have the option to stay? No, he must move to an adjacent vertex. So exactly that.
Then robber chooses r' to maximize val(r').
Thus f(r,c) = max_{r'∈N(r)} [ if r'=c then 0 else min_{c'∈N(c)} ( (c'==r'? 1 : 1 + f(r',c')) ) ].
That's the general recurrence.
Now we need to compute f(u,v) for tree up to 1e5. This recurrence looks like a typical minimax on a graph, but state space O(N^2) is too large. However maybe on trees, f can be expressed in terms of distances and something like "the distance from the robber's start to the farthest leaf beyond which the cop can't intercept?".
Let's try to find a simpler characterization.
Observation: On a tree, the unique simple path between r and c is important. In optimal play, the cop will always move along that path towards the robber, because any deviation would increase distance? But need to verify.
Consider the cop's move. Given positions after robber's move r' (distinct from c), cop wants to minimize eventual capture time. It's plausible that moving along the unique shortest path towards r' (i.e., reducing the distance) is optimal. Because moving away would increase distance, which is harmful. However, sometimes moving sideways might be optimal? For example, if the robber is at a leaf and the cop is one step away, moving towards is optimal. But could moving sideways yield a better position in future? On a tree, moving towards the robber along the unique path reduces the distance by 1. Any other move either increases distance (if moving away) or keeps distance same? Actually consider positions: c and r' are at distance d. The shortest path from c to r' goes through a neighbor p (the next vertex towards r'). If cop moves to p, distance becomes d-1. If cop moves to any other neighbor, the distance to r' becomes either d+1 (if moving away from r') or maybe d? Let's compute: In a tree, the distance from any neighbor of c (other than the one on the path to r') to r' is: distance from that neighbor to c is 1, plus distance from c to r' is d, but if that neighbor is not on the path, the path from that neighbor to r' must go through c, so distance = 1 + d (since you have to go back to c then to r'). So indeed distance becomes d+1. So moving away increases distance. So moving towards reduces distance, moving away increases. Since cop wants to minimize capture time, reducing distance is better. So moving along the shortest path (towards r') is optimal. Also, if there are multiple neighbors on the path? In a tree, from c to r' there is exactly one neighbor that lies on the path (unless c = r', but then they would be same, but we are in case r'≠c). So the cop has a unique best move: go towards r'. So min over c' is achieved by that neighbor, because any other neighbor gives a larger distance and likely longer capture time. But we need to verify if sometimes moving to a different neighbor could result in a lower eventual capture time despite larger immediate distance? Intuition: In zero-sum deterministic pursuit games on graphs, the cop's optimal strategy is to always decrease the distance (play "greedy") if the graph is a tree (or more generally if it's a "cop-win" graph). This is often true because distance is a potential function. So we assume cop always moves along the unique path towards robber.
Thus we can simplify: For given r and c (r ≠ c), after robber moves to r', if r' ≠ c, cop moves to the neighbor of c that lies on the path to r' (call it next(c, r')). Then the new state becomes (r', c') where c' = that neighbor. Then distance between r' and c' becomes? If originally distance between r and c is D, then after robber moves, the distance can change. Let's denote dist(r,c) = d. If robber moves away from cop along the path, he increases distance by 1: r' is the neighbor of r away from c (i.e., moving along the path from r to c? Actually path from r to c: r -- ... -- c. If robber moves to a neighbor that is on the path towards c, then r' is one step closer to c, so distance becomes d-1. If he moves to any other neighbor (i.e., not on the path to c), that neighbor lies in a subtree away from c; then the distance from r' to c is d+1 (since to go from r' to c, you have to go back through r: distance = 1 + d). So robber can either decrease distance by 1 (move towards cop) or increase distance by 1 (move away). But moving towards cop is dangerous because it may lead to immediate capture or reduce distance, which helps cop. Since robber wants to maximize time, he will move away (increase distance) if possible. However, if robber is at a leaf, moving towards cop might be forced (if leaf has only one neighbor, which is on the path to cop). So essentially robber will try to go as far away from cop as possible, i.e., move along the direction that increases distance.
Now, if cop always moves towards robber's new position r' along the unique path, then after the cop move, what is the new distance? Let's compute.
Let's denote:
Let d = dist(r,c). Consider the unique path between r and c. Let the neighbor of r on the path towards c be rc (next vertex from r to c). Similarly, neighbor of c on the path towards r is cr.
Now, robber moves to some neighbor r'. There are two types:
-
Type A: r' = rc (move towards c). Then new distance after robber move: d-1 (since distance decreases by 1). Then cop moves towards r' (which is now at distance d-1 from c? Actually after robber moves to rc, distance between rc and c is d-1. Cop chooses neighbor on path to rc. Since c is on path to rc? The path from c to rc goes through cr, etc. Actually cr is the neighbor of c towards r. Since r' = rc, which is the vertex adjacent to r along the path to c. The path from c to rc goes: c -> ... -> cr -> ... -> rc. The neighbor of c on that path is cr. So cop moves to cr. After that, distance between r'=rc and c'=cr? rc and cr are separated by the path of length? Actually the original path r - ... - c: r - (maybe some vertices) - c. The subpath between r and c has d edges. If r' = rc (the vertex adjacent to r towards c), and c' = cr (adjacent to c towards r), then the distance between r' and c' is d-2? Let's be precise: path: rc -- ... -- cr. That's d-2 edges? Because original path: r -- rc -- X1 -- ... -- Xk -- cr -- c, where total edges = d. Actually the path has vertices: r = v0, v1, v2, ..., vd = c, with v1 = rc, v_{d-1} = cr. So rc = v1, cr = v_{d-1}. So distance between rc and cr is (d-2) if d>=2. If d=1, then r and c are adjacent. Then rc = c? Actually if d=1, r and c are adjacent. Then rc is c (since neighbor of r towards c is c). But that would be r'=c, which is immediate capture (since robber moves onto cop). So for d>=2, r'=rc is not c. Then after cop moves to cr, the new distance = (distance between rc and cr) = d-2. So after a full round (robber then cop) where robber moves towards cop, the distance reduces by 2.
-
Type B: r' is any other neighbor of r (i.e., not on the path to c). Then r' is in a branch away from the path. After robber moves to r', distance from r' to c is d+1 (since you have to go back through r). Then cop moves towards r'. Which neighbor of c lies on path to r'? The path from c to r' goes: c -> ... -> r -> r'. Actually the shortest path from c to r' goes through r: c to r (d edges), then r to r' (1 edge) => d+1 total. The neighbor of c on that path is the same as the neighbor on the path to r, because to get to r' you must go towards r first. That neighbor is cr (the neighbor of c towards r). So cop moves to cr. Then new positions: r' (some branch) and c' = cr. What is the distance between r' and cr? Path: r' -> r -> ... -> cr. That's: r' to r =1, r to cr: distance between r and cr is d-1? Actually distance between r and cr is? Since cr is neighbor of c towards r, distance between r and cr is d-1? Let's compute: Path: r=v0, v1, ..., v_{d-1}=cr, v_d=c. So distance between r and cr = d-1. So distance from r' to cr = 1 + (d-1) = d. So new distance = d. Compare to original d: after robber moves away, and cop moves towards, distance remains d (if robber moves away to a branch). Actually we got new distance = d. So net effect: distance unchanged.
Check: d initial = dist(r,c). After robber moves away (branch), distance becomes d+1. Cop moves towards r' (to cr) reduces distance by 1 (since from c to cr is step towards r', distance from c to r' = d+1, after moving to cr distance becomes d). So distance ends at d. So it remains the same as initial.
Thus, from distance d (>=1), the robber has two types of moves: moving towards cop (if possible) reduces distance by 2 after both moves; moving away (to a branch) keeps distance same after both moves. However, moving towards cop could also lead to immediate capture if d=1? Because if d=1, moving towards cop would be moving onto cop (r'=c) -> capture before cop moves, resulting in 0 cop moves. That's a possibility but not desirable for robber if he wants to maximize time (since capture occurs immediately with 0 cop moves, which is worse than any positive number? Actually he wants to maximize the number of cop moves before capture. So he will avoid immediate capture if possible, unless all options lead to immediate capture? But if there are other moves (e.g., branches), he can choose those to avoid capture. If d=1 and robber is adjacent to cop, his possible moves: moving onto cop (capture with 0 cop moves) or moving away (to any other neighbor besides cop). If he has another neighbor, he can move away. Then after that, distance becomes? As per type B: if d=1 and robber moves to a neighbor not cop, then after robber move distance = 2 (since r' is neighbor of r away from c, distance from r' to c = 2 because r' to r =1, r to c =1 -> total 2). Cop then moves: cop's neighbor towards r'? Since c is adjacent to r, the path from c to r' goes through r: c -> r -> r'. The neighbor of c towards r' is r (since from c, the unique neighbor on path to r' is r, as c's neighbor r). So cop moves to r. Then new positions: r' and c'=r. Distance between r' and r is 1. So new distance = 1. So net effect: distance remains 1? Wait initial d=1, after away move and cop move, distance becomes 1. So indeed distance stays 1. So from d=1, after a round where robber moves away, distance becomes 1 again. So that can continue until robber maybe forced onto cop? But if robber can keep moving away, he can avoid capture? But eventually if robber is at a leaf? Let's analyze.
Now, if robber moves away (branch) and distance stays d, the game can potentially continue for many rounds, with the robber possibly moving further into a branch while the cop moves along the path. However, note that the positions change: the robber goes deeper into the branch, while the cop moves towards the robber's original position (the branching point). Over time, the cop may eventually catch up when the robber reaches a leaf and can't move further away.
Thus the game resembles: The robber tries to run towards a leaf far from the cop's starting position, while the cop chases. The capture time may be limited by the distance from the initial positions and the lengths of branches.
We need to compute number of cop moves until capture under optimal play.
Given the recurrence and the simplification that cop always moves towards robber, the game reduces to a kind of "distance" dynamic on the tree. We can define state by the two positions, but perhaps we can characterize the optimal strategy for the robber: He should always move away from the cop along the unique path leading to the farthest leaf (i.e., he should run to some leaf to maximize distance). Because moving towards cop reduces distance and speeds up capture. So robber's optimal move is to move to a neighbor that maximizes the distance to the cop after his move, but also considering that the cop will then move towards him. However, as we derived, moving away results in the same distance after the full round as initially, while moving towards reduces distance. So the robber should always move away if possible. However, if there is no "away" neighbor (i.e., r is a leaf, and the only neighbor is towards cop), then he is forced to move towards cop, which reduces distance.
Thus, if the robber is not at a leaf where his only neighbor is the parent (towards cop), he will move to a child (in a rooted sense) that leads away from cop. So effectively, the robber's strategy is to run away from the cop along the tree until he hits a leaf, then forced to turn back, and the cop will catch him after some number of moves.
But we need to be precise: The robber's move choice each turn might affect which branch he goes into, and the cop's move is deterministic (towards robber). This becomes like a one-player game where the robber picks a path to run, and the cop follows along the path. The robber wants to maximize the number of cop moves (i.e., total rounds). The cop's moves count is the number of steps she takes until they meet.
We can try to compute the capture time in terms of distances and the structure relative to the initial positions.
Consider the tree rooted at the cop's starting position v? Or maybe consider distances from v. The cop starts at v. The robber starts at u. They alternate moves with robber first. Cop always moves towards robber along the unique path. This is reminiscent of a pursuit where the robber runs to the farthest leaf from the cop, but due to the alternating moves, the cop may cut off some branches.
Let's define the distance from v to each node: dist_v(x). Similarly, from u.
We can consider the game as a race: The robber wants to maximize the number of moves before the cop reaches his position. Since the cop moves each turn (after robber), the relative speed? In a continuous sense, if both moved simultaneously, the distance would close by 2 per round if they move towards each other, or stay same if robber runs away. Here because robber moves first and cop after, the net effect is that if robber runs away, distance remains same after each full round. However, the robber may eventually be forced to run towards the cop when hitting a leaf, which then reduces distance.
So the robber will run to some leaf that is farthest from the cop, but the cop is also moving. If the robber runs along a path from u to some leaf L, what happens? Initially, distance between u and v is d0 = dist(u,v). As the robber moves away from v along the path from u to L (which presumably goes away from v), the distance after each robber move increases by 1, then cop move decreases by 1, so net distance stays d0? Actually careful: Suppose the robber moves along a path that goes away from v. The direction of "away" means moving to a neighbor that increases distance from v. If robber moves to a node that is farther from v than u (i.e., dist_v(r') = dist_v(r) + 1), then after robber move, distance to cop = dist_v(r')? Actually distance between r' and c is not necessarily dist_v(r')? Since c is at v? Not necessarily; cop moves after robber. But let's track positions.
It may be easier to transform the game into a simpler one: Since cop always moves towards robber along the unique path, we can think of the cop's position as always on the path from v to the robber's current position. Actually, is that true? Initially, cop at v, robber at u. The path from v to u is unique. After robber moves to r' (away from v along a branch), the cop moves to the neighbor on the path from v to r'. Since v is the starting point, the neighbor on that path is the one towards u (since r' is in some branch off the path from v to u? Actually if robber moves away, r' is in a subtree that is not on the original v-u path? Let's analyze.
We can root the tree at v (cop start). Then we have distances from v. Initially robber at u, with distance du = dist(v,u). The unique path from v to u passes through some nodes. Now, if robber moves away from v, he must move to a child (in the rooted tree) that is further from v? But careful: The robber's move to a neighbor can be either towards v (parent) or away (to a child) in the rooted tree at v. However, note that the robber might also move to a neighbor that is not strictly a child in the v-rooted tree if that neighbor is actually closer to v? In a rooted tree at v, each node (except v) has a parent towards v. The neighbors are either parent (closer to v) or children (farther from v). So moving towards v reduces dist_v by 1; moving away increases dist_v by 1.
Now, if robber moves away (to a child), his dist_v increases by 1. After his move, cop moves. Cop's move: he moves towards robber along the unique path. In the v-rooted tree, the path from v to the robber's new position r' is: from v up to the LCA etc. Since r' is deeper in some subtree, the path goes through the parent of r' which is the robber's previous node r (since r' is a child of r). Actually if r' is a child of r (away from v), then the path from v to r' goes through r. So the neighbor of v that is on the path to r' is the same as the neighbor on the path to r, which is the child of v along the path? But v might not be directly adjacent to r. Actually the cop is currently at some node c (not necessarily v after some moves). But let's consider the first move: cop starts at v. After robber moves to r' (child of u maybe), cop moves to the neighbor on the path from v to r'. That neighbor is the next step from v towards u (since r' is in the subtree of u? Actually r' is a neighbor of u away from v, so u lies on the path from v to r'. The path from v to r' is v ... u ... r'. So the neighbor of v on that path is the child of v that leads to u (the next node after v on the path to u). So cop moves one step along the path from v to u. So after first cop move, cop becomes some node along the v-u path, one step closer to u. In subsequent rounds, the cop will always be on the path from the original v to the current robber position, moving step by step towards the robber.
Thus after each cop move, the cop's position is on the unique shortest path between v (original) and the robber's position? Not exactly, because cop moves after robber, but indeed, if the cop always moves along the shortest path to the robber, then after each full round, the cop will be one step closer to the robber along that path. Actually we need to track.
Given the dynamics, it seems the game reduces to a chase along a path where the robber can delay by going into branches, but the cop gradually approaches the branch point.
Maybe we can think in terms of distances: Let's denote d(t) = distance between robber and cop after the robber's move? Or after each full round? Let's define after the cop's move (i.e., at start of robber's turn) the distance is D. At that point, it's robber's turn. Initially (before any moves) it's robber's turn, distance = dist(u,v) = D0.
Now robber moves. He can choose a neighbor. Two types:
-
Move towards cop: reduces distance by 1 (since moves one step towards cop, so new distance = D-1). Then cop moves: cop moves towards robber, reducing distance by another 1? Actually if robber moved towards cop and they are not colliding (i.e., D>1), then after robber move distance = D-1. Cop then moves towards robber along the shortest path; since they are on a line, moving towards reduces distance by 1, so new distance = D-2. So after full round, distance becomes D-2.
-
Move away from cop: increases distance by 1 => distance = D+1. Cop moves towards, reducing distance by 1 => new distance = D. So after full round, distance stays D.
If robber moves onto cop (D=1 and moves towards) then capture occurs immediately, distance = 0 after robber move, game ends, cop moves 0.
Thus, if D > 1, robber can always move away? He can move away only if there exists a neighbor that is not towards the cop. That is, if the robber is not at a leaf in the tree when considering the direction away from cop? More precisely, from his position, the neighbor towards the cop is the one that reduces distance (the parent in the rooted tree with cop as root). The other neighbors increase distance. So as long as the robber has degree > 1 (or at least one neighbor that is not towards the cop), he can move away. If robber is at a leaf, he has only one neighbor (the parent towards cop), so he is forced to move towards cop.
Therefore, the game proceeds as follows: Starting distance D0. While the robber is not at a leaf (i.e., has an away move), he will move away, keeping distance constant after each full round. Meanwhile, the cop moves one step towards the robber each round, which means the cop is getting closer to the robber's initial position along the path. But because the robber moves away, he is moving further into a branch, so the distance remains constant. However, the positions shift: robber goes deeper into a branch, cop advances along the path towards the branching point.
Eventually, the robber will reach a leaf. At that moment, his only move is towards the cop (i.e., back along the branch). At that point, distance D will be some value (maybe the length of the branch from the leaf to the cop's current position). Then when robber moves towards cop, distance decreases by 1 (or capture if adjacent). Then cop moves towards, decreasing distance by 1 again. So distance reduces by 2 per round thereafter, until capture.
But we must be careful: After the robber reaches a leaf, he may not be forced to move towards cop immediately? Actually if he is at a leaf, his only move is to the parent (which is towards the cop). So he must move towards cop. So his next move will be towards cop. That will reduce distance. However, after that move, he might no longer be at a leaf; he could then possibly move away again? For example, if after moving back from leaf, he arrives at a node that has other branches (children) not yet visited? But note that the cop is approaching from the other side. The robber could perhaps move away into another branch? But after moving back towards cop, distance reduces, and the robber is now at a node that might have other children besides the leaf he came from. However, those children are away from cop? Let's think: The robber at leaf L moves back to its parent P. At that point, the cop is somewhere on the path from v to P (maybe approaching). The direction towards cop from P is its parent (towards v). The other children of P (excluding L) are away from cop. So if P has other children besides L, the robber could then move away again into one of those children. That would again keep distance constant. So the robber might be able to "hop" between branches to prolong the game. But is that beneficial? Possibly yes, because moving away again maintains distance rather than reducing it. So the optimal strategy for the robber is to avoid moving towards cop as long as possible. That means he should always move to a neighbor that is away from the cop, if such exists. So he will only move towards the cop when forced (i.e., when at a leaf with no other away moves). But after moving towards cop from a leaf, he arrives at its parent, which may have other away branches (children) that are not towards the cop. Then he can again move away. So he might not be forced to reduce distance until he reaches a node where all children are "blocked" or something. However, the cop is also moving each round, getting closer. The robber's ability to move away may be limited because the cop may eventually reach the branching point, at which point moving away might be impossible because the robber would run into the cop? But we need to simulate.
Actually, consider the tree as rooted at v (cop start). At any state, we can define positions relative to this root. Let's denote the cop's position c (some node). The robber's position r. The unique path from v to r goes through c? Not necessarily; c is somewhere on that path? Since cop always moves towards robber along the shortest path, and initially c = v. After the first cop move, c moves one step along the path from v to r (the robber's position at that time). As the game proceeds, it is plausible that the cop always stays on the unique path from v to the robber's current position. Let's test: Initially c=v on the path to r=u. After robber moves to r' (some neighbor), the path from v to r' goes through the original path to u and then maybe a branch. The cop moves to the neighbor on that path from c to r', which is the next step along the path from c to r'. Since c was on the path from v to r, and the new path from v to r' may share the initial segment up to some point. It might be that c remains on the path from v to r' after moving? He moves one step along that path, so yes, after moving, c' will be on the path from v to r'. So invariant: after each cop move, the cop is on the unique shortest path from v to the robber's current position. Moreover, the distance from v to the cop along that path is non-decreasing? Actually cop moves each turn, but always towards the robber, which may increase or decrease his distance from v? Let's think: cop is moving along the path from v to robber. As he moves towards robber, he moves away from v if the robber is not in the subtree of v? Actually v is the root, the path from v to robber goes outward from v, so moving towards robber means moving away from v, i.e., increasing depth (distance from v). So cop will always move away from v (i.e., deeper into the tree) because robber is always somewhere in the tree (not at v). Could cop ever move back towards v? If robber moves towards v, then the path from v to robber may become shorter, and the cop's move towards robber might actually be moving towards v? Let's consider: Suppose robber moves towards v, i.e., reduces his distance from v. Then the path from v to robber is a prefix of the previous path. Cop is currently somewhere on that path, but possibly further from v than the robber? Actually if robber moves towards v, he might get closer to v than the cop. For example, initial: v is root, u is some node. Suppose robber moves towards v (parent). Then distance from v to robber decreases. Cop, who was on the path from v to robber before the move, might be ahead (further from v) than robber after robber's move? Let's simulate: v is at depth 0. Suppose initial r at depth d, c at depth 0 (v). After robber moves towards v, r' depth d-1. Cop then moves towards r' along the path. The path from v to r' is just the nodes up to depth d-1. The neighbor of c (v) on that path is the child that leads to r'? Actually from v, to go to r', you go along the unique path: v -> ... -> r'. The first step is the child of v that is ancestor of r'. So cop moves from v to that child, increasing depth from 0 to 1. So cop moves away from v, not towards. So cop never moves towards v; he always moves away from v (or possibly stays? Could he stay? No, he must move to an adjacent vertex. The only way to move towards v is if his current node has v as neighbor and he moves to v. But v is the root, and if robber is somewhere else, the shortest path from c to robber might go through v? For c not at v, moving to v would be moving away from robber if robber is not on that side. Since cop wants to minimize capture time, moving towards robber is optimal. So cop will move along the path from c to robber. That path may sometimes go through the parent (towards v) if the robber is in a different branch? But note invariant: cop is on the path from v to robber. Because he always was, and he moves along the path from his current position to robber. If cop is on the path from v to robber, then the direction from c to robber is always away from v (deeper) because robber is further from v than c? Is that always true? If cop is on the path from v to robber, then the distance from v to robber = dist(v,c) + dist(c, robber). Since both distances are nonnegative. The path from c to robber goes away from v (i.e., increasing depth) because the path from v to robber passes through c and then goes outwards. So indeed, from c, moving towards robber means moving to a child (farther from v). So cop always moves away from v, increasing his depth.
Thus, cop's depth (distance from v) strictly increases each time he moves (until maybe capture). Robber can move towards v (decreasing his depth) or away (increasing depth). But robber wants to delay capture, so he will try to increase his depth as much as possible, but sometimes forced to decrease.
Now consider the game in terms of depths and distances. Because the cop always moves towards robber, after each full round, the cop's depth increases by 1 (if he moves). Actually each cop move increases his depth by 1 (since he moves to a child). So after k full rounds (i.e., k cop moves), the cop's depth = k (since starting depth 0). Provided he hasn't caught the robber yet. This is a key observation: Starting at v (depth 0). After first cop move, depth = 1; after second, depth = 2; etc. Because each cop move goes to a child along the path to robber, and since robber is always at a depth >= current cop depth? Not necessarily: robber could move towards v and become at depth less than cop's depth. But if robber moves towards v, then after robber's move, his depth might be less than cop's depth. Then the path from cop to robber would go upwards (towards v). In that case, cop moving towards robber would move to its parent (towards v), decreasing depth. But does that happen under optimal play? Let's test scenario: Suppose robber moves towards v, reducing his depth. Could that happen under optimal play? The robber wants to maximize time, so moving towards v reduces distance and may lead to quicker capture? Possibly not optimal unless forced. But is it ever optimal for robber to move towards v? Considering the alternative: If robber is at a node that has no child (away moves) except the parent, then moving towards v is forced. But if there is at least one child (away move), robber will choose that to keep distance constant. So unless robber is at a leaf, he will not move towards v. However, after moving away repeatedly, he may reach a leaf. Then he is forced to move towards v (back to parent). At that point, his depth decreases by 1. Meanwhile, cop's depth is increasing each round (since he always moves away). So when robber is at leaf and moves back, his depth becomes depth_leaf -1. Cop's depth at that moment is some value. Could cop's depth be greater than robber's depth after that move? Possibly. Let's examine.
Suppose robber starts at u, with depth du = dist(v,u). He moves away each round, going deeper into a branch. So his depth increases by 1 each time he moves away (since moving to child). Cop moves after each robber move, increasing depth by 1. So after each full round where robber moves away, both depths increase by 1 (robber +1, cop +1). So the difference in depths? Initially, robber depth = du, cop depth = 0. After first robber away move: robber depth = du+1; cop moves: cop depth = 1. After second away round: robber depth = du+2; cop depth = 2. So after k away rounds, robber depth = du + k, cop depth = k. So the difference (robber depth - cop depth) remains constant = du. So as long as robber moves away, the cop is always exactly du steps behind the robber along the path from v? Actually the path from v to robber: cop is at depth k, robber at depth du + k. So the distance between them along the path is (du + k) - k = du. So distance remains du.
Now when robber reaches a leaf, he cannot move away further. At that leaf, his depth is some L (maybe du + k). The leaf has no children, so his only move is to parent, decreasing depth by 1. But note: At the moment before robber moves, it's robber's turn, with positions: robber at leaf (depth L), cop at depth k (with L = du + k). Distance between them along path = du. Now robber moves to parent (depth L-1). This reduces his depth by 1, so after robber move, his depth = L-1, cop depth still = k. Then distance becomes? The path from cop (depth k) to robber (depth L-1) may be: cop is at depth k, robber at depth L-1. Since L-1 = du + k -1. The distance along the path = (L-1) - k = du -1. So distance decreases by 1. Then cop moves towards robber. Since robber is now at depth L-1, and cop at depth k, the shortest path goes from cop up? Actually cop is on the path from v to robber? Is cop on that path? Initially, before robber's move, cop was on the path from v to the leaf (since robber was at leaf and cop was on that path at depth k). After robber moves to parent, the parent is also on that same path, and cop is still on that path at depth k. So cop is on the path from v to the parent as well. So the path from cop to robber goes from depth k up to the parent (depth L-1). Since k < L-1, the direction is deeper, so cop will move to the child at depth k+1 (the next node towards robber). So cop depth becomes k+1. After cop move, robber depth = L-1, cop depth = k+1, distance = (L-1) - (k+1) = du -2. So net after full round when robber is forced to move towards cop, distance reduces by 2.
Now after that, robber may be at a node (parent) that might have other children (branches) besides the leaf he came from. He can then choose to move away again into another child, provided such child exists. If he does, the distance will remain constant (du-2) for subsequent rounds? Let's see: After the cop move, distance = du-2. Then robber's turn. He can move away to a child (that is not towards the cop). Which child? The children of the parent node include the leaf he just came from (which is towards the leaf he already visited) and possibly others. If he moves to a different child (that is not on the current path to cop? But cop is at depth k+1, which is on the path from v to the parent. The parent node is at depth L-1. The children of the parent (other than the one leading to the cop) are nodes at depth L. If robber moves to any child, his depth increases by 1 to L (which might be the leaf or another leaf). But moving to a child that is not the cop direction (i.e., any child except the one that leads back to v) is an away move? However, note that the cop is at depth k+1, which is on the path from v to the parent. The direction from parent to cop is the parent of parent (i.e., towards v). So all children of the parent are away from v, so they increase depth. So moving to any child (including the previous leaf) is an away move relative to cop? But careful: The distance calculation for away move: If robber moves to a child, distance from robber new position to cop? Initially distance = du-2. After robber moves to child, distance becomes? Robber moves away from cop? Let's compute: rob at parent (depth L-1), cop at depth k+1, distance = du-2. If robber moves to child, depth becomes L. Distance from child to cop = (L) - (k+1) = (du + k -1) - (k+1)? Actually recall L = du + k. So L-1 = du + k -1. Then child depth = L = du + k. Then distance = (du + k) - (k+1) = du -1. So distance increases by 1 relative to before robber move (which was du-2) to du-1. Then cop moves towards robber: cop will move to depth k+2 (since he moves deeper along path), distance becomes (du + k) - (k+2) = du -2. So net distance after full round returns to du-2. So moving to a child (away) keeps distance constant after full round (since initial distance du-2, after robber away move becomes du-1, after cop move becomes du-2). So indeed, after the forced move towards, if robber can move away again, distance will stabilize at the new constant (which is du-2). So the pattern: As long as robber has an "away" move, distance remains constant. When he is forced to move towards (i.e., at a leaf), distance decreases by 2 after the full round (the robber's forced towards move plus cop's move). After that, if the robber can again move away, distance remains at the new constant. So overall, the distance between them (when measured at the start of robber's turn) only decreases when the robber is at a leaf and forced to move towards cop. Otherwise, distance stays same.
Thus, the game proceeds as: The robber starts at distance D0 = dist(u,v). He will try to maximize the number of rounds (i.e., cop moves) before distance eventually reaches 0 (capture). The distance decreases only when the robber is forced to move towards the cop, i.e., when he is at a leaf. Each such forced step reduces distance by 2. So the total number of times distance decreases is D0/2? But distance may not be even; capture can happen when distance becomes 0 after robber's move (if D=1 and robber moves onto cop) or after cop's move (if D=2? Actually let's analyze endgame). So the total number of cop moves is roughly (number of rounds where distance stays constant) + (number of cop moves during decrease phases). But we need to compute exactly the total number of cop moves until capture.
Alternatively, we can think in terms of the robber's path: He will try to go to a leaf far away, but the cop is chasing along the same branch. Eventually the cop will catch him when they meet at some node. The capture occurs when the robber moves onto the cop (if they are distance 1 and robber moves towards cop) or when the cop moves onto the robber (if they are distance 1 and cop moves towards robber). Typically, capture will occur after a cop move when distance becomes 0.
Let's try to simulate optimal play: Robber's goal: maximize number of cop moves. Cop's goal: minimize.
Given the tree and starting positions, what is the optimal leaf for the robber to run to? He could choose any leaf reachable from his starting position while avoiding the cop. But the cop is moving towards him each turn. The robber can only move one step per turn. So it's like a race: The robber wants to maximize the number of steps before cop catches him. This is reminiscent of the concept of "escape distance" or "maximum distance the robber can maintain