Traveling around the country
Limits: 3 sec., 512 MiB
This statement was translated automatically from Ukrainian.
Zenik lives in a country with \(n\) cities. There are \(n - 1\) roads in the country, each connecting two cities. All cities are connected by roads in such a way that there is only one route between any two cities.
The capital of the country is the city with number 1. Roads lead from the capital to other cities, from them — even further, and thus a network of cities branching out from the center is formed.
Zenik can travel around the country in two ways:
Take a marshrutka (minibus) between two cities connected by a road.
Teleport further from the capital — instantly get to any city that is one level further from the capital than the one he is currently in.
There are \(q\) queries: what is the minimum number of steps Zenik needs to reach Marichka if he is currently in city \(u\), and she is in city \(v\).
Input
The first line gives an integer \(n\) — the number of cities in the country.
The next \(n - 1\) lines contain two integers each — pairs of cities connected by a road.
The next line contains a single integer \(q\) — the number of queries.
The next \(q\) lines contain two integers \(u\) and \(v\) each — the cities where Zenik and Marichka are, respectively.
Output
In \(q\) lines, output the minimum number of steps for Zenik to get from city \(u\) to city \(v\).
Constraints
\(1 \le n, q \le 4 \cdot 10^5\),
\(1 \le u, v \le n\).
Scoring consists of the following blocks:
1 point for each example from the statement,
27 points: \(n, q \le 1000\),
14 points: cities \(u\) and \(v\) are at the same distance from the capital,
21 points: city \(u\) is not further from the capital than city \(v\),
37 points: no additional constraints.
You will receive the points for a block only if your program passes all tests in that block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 1 2 1 4 2 3 2 5 4 6 6 7 4 5 7 4 7 7 4 4 4 | 1 2 2 0 |
Notes
The country in the example looks like this:
In the first query, Zenik is in city 5, and Marichka is in city 7. Zenik can teleport from city 5 to city 7 in one step, since city 5 is at a distance of 2 from the capital, and city 7 is at a distance of 3.
In the second query, Zenik can take a marshrutka from city 7 to city 6, since there is a road between them, and then from city 6 take a marshrutka to city 4. Here he needs to take 2 actions.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Traveling around the country - Editorial
Let \(dist_x\) be the distance
(level) of vertex \(x\) from the
capital (city 1), that is, the number of edges on the path from 1 to
\(x\). As preprocessing, one DFS from
root 1 computes \(dist_x\) for all
vertices and records the entry and exit times
(tin/tout) to check the ancestor/descendant
relation.
Consider the cases for a single query \((u,v)\):
If \(dist_u < dist_v\), it is always better to move forward by teleports (there is no need to go down a level): each teleport increases the level by 1, so exactly \[dist_v - dist_u\] steps are needed.
Otherwise (\(dist_u \ge dist_v\)), there are two strategies:
If \(v\) is an ancestor of \(u\) in the tree (that is, \(v\) lies on the path from \(u\) to the root), we can climb along the parents to \(v\) using roads only. This takes \[dist_u - dist_v\] steps, and it is optimal, because teleports only move down away from the center, while we need to go up.
If \(v\) is not an ancestor of \(u\), there is no path to \(v\) that only goes up. However, we can go up some number of steps (to end up at level \(dist_v\) or \(dist_v + 1\)) and then teleport down from level \(dist_v\) right to \(v\). The simplest optimal scheme is to go up from \(u\) by \(dist_u - dist_v\) steps to any vertex at level \(dist_v\), and then reach \(v\) in two steps (one level up and a teleport down). So we get \[dist_u - dist_v + 2.\]
So the final formula for the answer to a query \((u,v)\) is: \[\mathrm{ans}(u,v)=\begin{cases} dist_v - dist_u, & \text{if } dist_u < dist_v,\\[6pt] dist_u - dist_v, & \text{if } dist_u \ge dist_v \text{ and } v \text{ is an ancestor of } u,\\[6pt] dist_u - dist_v + 2, & \text{otherwise.} \end{cases}\]
To check in \(O(1)\) whether \(v\) is an ancestor of \(u\), the DFS from the root stores the entry and exit times \(tin_x\) and \(tout_x\) of every vertex. Then \[v \text{ is an ancestor of } u \quad \Longleftrightarrow \quad tin_v \le tin_u \text{ and } tout_v \ge tout_u.\]
The total complexity is \(O(n)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|