#P4103. Building New Channels Over a Tree Network
Building New Channels Over a Tree Network
Building New Channels Over a Tree Network
In this problem, the country’s road network is modeled as a tree with unit edge weights, and cities are located at the vertices. The cost of building a new channel between any two cities a and b is defined as the length of the shortest path between a and b in the tree (i.e. the number of edges on the unique path between a and b, which can be written in LaTeX as ).
For each plan, you are given a set of distinct vertices. For every pair of these vertices, a new channel is constructed, so a total of (\binom{k}{2}) channels are built. Your task is to compute the following for each plan:
- The sum of the construction costs of all the new channels.
- The minimum construction cost among all the new channels.
- The maximum construction cost among all the new channels.
If , output 0 for all three values.
inputFormat
The input begins with an integer , the number of vertices in the tree. Then follow lines each containing two integers and , describing an edge between vertices and . It is guaranteed that the given graph is a tree with unit edge weights.
Next, an integer is given, representing the number of plans. Each plan is described as follows:
- An integer (number of selected vertices).
- A line with space-separated integers indicating the selected vertices.
Note: The vertices are 1-indexed.
outputFormat
For each plan, output three space-separated integers on a new line: the total cost of building channels, the minimum channel cost, and the maximum channel cost. If there is no channel (i.e. when ), output "0 0 0".
sample
5
1 2
1 3
3 4
3 5
1
2
2 4
3 3 3