Finding a Tree Connected Component
Time limit1sMemory limit1024 MB
Given a rooted tree and several vertex sets, report the vertex count and vertex-number sum of the smallest connected subtree containing each set.
Problem
A tree with vertices and undirected edges is given. Vertices are numbered from to , and vertex is the root. Perform the following query times.
- ... : Let be the connected component with the fewest vertices that contains all vertices . Print the number of vertices in and the sum of the vertex numbers in , separated by a space.
Input
The first line contains . ()
The next lines each contain a parent vertex and a child vertex , separated by a space. The given graph is a tree. (, , )
The next line contains . ()
The next lines each contain , separated by spaces. The sum of all is at most . (, (), ())
All numbers in the input are integers.
Output
For each query, print the answer on its own line.