Domain: Algorithms & Data Structures

An Arbor Network is an undirected, connected, acyclic graph (i.e., a tree) with N junctions numbered 1 through N.

Every morning, R rival couriers traverse the network simultaneously. Rival i starts at junction s_i and travels to junction t_i along the unique shortest path in the tree. If that path is the vertex sequence p_1, p_2, ..., p_k with p_1 = s_i and p_k = t_i, then at integer time j (for 1 ≤ j ≤ k) the rival is exactly at vertex p_j.

After a rival reaches t_i, they stop moving and cannot be encountered thereafter. Concretely, for rival i, encounters are only possible at times 2, 3, ..., k (while the rival is still traveling); times after reaching the destination are ignored for collision purposes.

A protagonist courier starts at junction A at time 1 and must reach junction B (A ≠ B). At each integer time step, the protagonist may either:

move along one incident road to an adjacent junction, or
wait at the current junction.
A collision occurs if at any integer time t the protagonist and any rival occupy the same junction. (Being on the same edge segment at the same time is allowed.)

It is guaranteed that s_i ≠ A for all rivals i.

Goal:
For each test instance, compute the earliest integer time at which the protagonist can arrive at junction B without ever colliding at a junction with any rival courier. If it is impossible, output -1.

Input:
The first line contains an integer T, the number of test instances.

Each test instance:

One line with four integers N, R, A, B (2 ≤ N ≤ 10^5, 1 ≤ R ≤ 200, 1 ≤ A,B ≤ N, A ≠ B)
Next N-1 lines: each line has two integers u and v describing an undirected edge of the tree
Next R lines: each line has two integers s_i and t_i with s_i ≠ t_i and s_i ≠ A
It is guaranteed that the sum of all N across test instances does not exceed 10^5.

Output:
For each test instance print one integer: the earliest collision-free arrival time at B, or -1.

Grading rubric:
criterion: Does the answer explicitly state the finite search horizon bound t* ≤ 2N + 1?
weight: 1
criterion: Does the answer explicitly define a forbidden pair (u, t) as: some rival occupies vertex u at integer time t during its travel?
weight: 1
criterion: Does the answer explicitly state the forbidden-pair budget: the total number of forbidden (vertex, time) pairs is at most N·R?
weight: 1
criterion: Does the answer explicitly define the base frontier/DP set as S1 = {A}?
weight: 1
criterion: Does the answer explicitly state the waiting transition: if v ∈ S_t and (v, t+1) is not forbidden, then v ∈ S_{t+1}?
weight: 1
criterion: Does the answer explicitly state the movement transition: if u ∈ S_t, v is adjacent to u, and (v, t+1) is not forbidden, then v ∈ S_{t+1}?
weight: 1
criterion: Does the answer explicitly state that the reported arrival time equals min{ t : B ∈ S_t}?
weight: 1
criterion: Does the answer explicitly state the failure rule: output −1 if there is no t ≤ 2N + 1 such that B ∈ S_t?
weight: 1
criterion: Does the answer explicitly state the combinatorial bound that each vertex can enter the frontier S_t at most R + 1 times over all t?
weight: 1
criterion: Does the answer explicitly state the overall time complexity bound O(N·R)?
weight: 1

Answer:
Explicit finite time horizon: t* ≤ 2N + 1
Let t* be the earliest arrival time at B among all collision-free schedules, if any exist.

Claim. If a collision-free schedule exists, then t* ≤ 2N + 1.

Reason (sketch). Each rival walks a simple tree path of length at most N, so all rivals have finished moving by time ≤ N, hence encounter constraints cease no later than time N + 1. From time N + 1 onward, the protagonist can ignore rivals and reach any node (in particular B) within at most N more moves in a tree. Therefore some collision-free schedule (if one exists at all) can be made to arrive by (N + 1) + N = 2N + 1. So we only need to search times t ≤ 2N + 1.

Forbidden pairs and the budget ≤ N · R
Define a vertex-time pair (u, t) as forbidden if at time t some rival occupies vertex u at a time when encounters count (i.e., along their travel times 2 through the path length).

Each rival visits at most N vertices over time, hence contributes at most N forbidden pairs. Therefore the total number of forbidden pairs satisfies:
{(u, t) forbidden} ≤ N · R.

Store forbidden information as per-time lists: forbidden[t] = list of vertices forbidden at time t. Total stored entries is O(NR).

Formal DP / frontier recurrence (explicit)
Let S_t ⊆ {1, ..., N} be the set of vertices where the protagonist can be at time t without having collided at any time ≤ t.

Base:
S_1 = {A}.

Transition (for every t ≥ 1): for each vertex v,
v ∈ S_{t+1} ⇔ ((v, t+1) is not forbidden) AND (v ∈ S_t OR ∃u ∈ N(v) with u ∈ S_t).
This exactly models “wait or move one edge” while avoiding forbidden vertex-time collisions.

Output rule (explicit):
answer = min { t : B ∈ S_t },
and output -1 if no such t exists with t ≤ 2N + 1.

Key invariant: each vertex enters at most R + 1 times
Use an incremental frontier computation (do not build a full N × (2N) DP table).

Invariant (explicit). Over all times, any fixed vertex v can become newly feasible (i.e., “enter” the frontier S_t) at most R + 1 times.

Justification (sketch): a vertex can be removed from feasibility at time t only if it is forbidden at t. A given vertex can be forbidden by at most R rivals (each at some time), hence it can be “kicked out” at most R times, so it can enter at most R + 1 times.

Incremental processing and total work bounds
Process time steps t = 1, 2, ..., 2N + 1 while maintaining the current feasible set S_t (as a boolean in[v]) and a candidate list for membership tests.

Type-1 removals handled incrementally (explicit). At time t, mark all vertices in forbidden[t] as blocked for time t. The total amount of such removal handling over the whole run is:
∑_t |forbidden[t]| ≤ N · R.

Neighbor-announcement bound (explicit). Each time a vertex v enters the frontier, we may “announce” its neighbors as candidates for the next time. Since v enters at most R + 1 times, the total neighbor-announcement work is bounded by:
∑_v deg(v) · (R + 1) = (R + 1) ∑_v deg(v) = (R + 1) · 2(N − 1) ≤ 2N(R + 1).

Total candidate processing (explicit). Candidates arise only from:

forbidden-event triggers (bounded by N · R), and
neighbor announcements (bounded by 2N(R + 1)).
Thus, the total number of candidate checks processed over all times is O(NR).
Final complexities (explicit)
Time complexity: O(NR), justified by combining:

forbidden-event processing ≤ N · R, and
neighbor announcements ≤ 2N(R + 1),
plus O(1)-amortized candidate checks per announced item.
Memory complexity: O(NR) for storing forbidden per-time lists (total entries ≤ N · R) plus O(N) for the tree adjacency and frontier arrays; overall O(NR).