#P8004. Optimizing Train Route Distances with Teleportation Portals
Optimizing Train Route Distances with Teleportation Portals
Optimizing Train Route Distances with Teleportation Portals
In country M, there are (n) cities connected by (n-1) bidirectional highways forming a tree, meaning that every city is reachable from every other city. The country has developed top-notch technology allowing it to place at most (L) pairs of teleportation portals along the highways. Each portal pair consists of two portals, each having a front and a back side. When a train running along a highway meets a portal, it is teleported immediately: if it enters via the front side it exits from the front side of the paired portal, and similarly for the back side. Portals can be placed at any point along a highway (subject to not placing more than (L) portals on any single highway).
The distance (dis(i,j)) from city (i) to city (j) is defined as the number of cities the train stops at along its route (excluding the starting city but including the destination).
There are (m) important cities (x_1, x_2, \dots, x_m). Your task is to design a scheme to place at most (L) pairs of portals such that the sum of distances from the capital (city 1) to each of the important cities, (\sum_{i=1}^{m} dis(1,x_i)), is minimized. The placement must guarantee that all cities remain reachable from each other. Note that if placing any portals is not beneficial, you may choose to place zero pairs. (Due to some specifics of the special judge, do not place more than (L) portals on any single highway, otherwise your submission will be judged as WA.)
inputFormat
The first line contains three integers (n), (L) and (m) ((2 \leq n \leq 10^5,\ 0 \leq L \leq 10^5,\ 1 \leq m \leq n)), representing the number of cities, the maximum number of portal pairs allowed, and the number of important cities respectively.
The following (n-1) lines each contain two integers (u) and (v) representing a bidirectional highway between cities (u) and (v).
The last line contains (m) distinct integers (x_1, x_2, \dots, x_m) indicating the important cities.
outputFormat
The output should consist of two parts:
- On the first line, output the minimum possible sum of distances (\sum_{i=1}^{m} dis(1,x_i)) under your chosen portal placement.
- On the second line, output an integer (P) ((0 \leq P \leq L)) denoting the number of portal pairs you decided to place. (If (P = 0), no further output is required.)
Note: You are allowed to output a valid scheme that does not place any portals. This trivial scheme is guaranteed to preserve connectivity and will yield the sum of distances in the original tree.
sample
5 1 2
1 2
2 3
2 4
1 5
3 4
4
0
</p>