Tree Tripping
Time limit2sMemory limit1024 MB
Given a tree and queries that each list k nodes, find the node whose total distance to the listed nodes is smallest and print that sum.
Problem
A tree with nodes is given. Process queries of the following form.
k u_1 u_2 ... u_k: Choose any node in the tree. Print the minimum possible sum of distances from the chosen node to nodes .
Input
The first line contains the number of nodes and the number of queries , separated by a space. (; )
The next lines each contain two distinct node numbers and , separated by a space, which form an edge of the tree. ()
The next lines each contain a query k u_1 u_2 ... u_k. (; ; implies )
The sum of over all queries is at most .
Output
For each query, print the minimum sum of distances between the chosen node and the given nodes, one per line.