#P3233. World Tree Administration
World Tree Administration
World Tree Administration
The World Tree is an enormous tree whose branches form the entire world. In this land, various races and creatures live together abiding by the creed of the goddess Allison, who upholds absolute fairness and justice. Their belief is that fairness is the fundamental cornerstone that keeps the World Tree alive and in full operation.
The structure of the World Tree can be modeled mathematically. There are \( n \) races living in settlements numbered from \( 1 \) to \( n \) (each race lives in the settlement with the same number as its identifier). The settlements are connected by bidirectional roads of length \(1\), such that the connections form a tree (i.e. any two settlements can reach each other, and there is no cycle). The distance between two settlements is defined as the length (i.e. number of roads) of the unique simple path connecting them.
In the \( i \)th year, the king of the World Tree authorizes \( m_i \) settlements to act as temporary assembly halls. For any race \( x \) (with \( x \) as its identifier), let the nearest temporary assembly hall be at settlement \( y \) (with \( y \) being one of the authorized settlements). In case there are multiple temporary assembly halls at the same minimum distance from \( x \), the race will be governed by the one with the smallest settlement number among them.
Your task is to assist the king by computing, for each year, how many races each temporary assembly hall will govern (note that a hall governs the race of its own settlement as well).
Input/Output Format: Detailed below.
inputFormat
The input begins with an integer \( n \) representing the number of settlements. The next \( n-1 \) lines each contain two integers \( u \) and \( v \) denoting that there is a bidirectional road connecting settlements \( u \) and \( v \).
The following line contains an integer \( q \) indicating the number of years (queries). Each of the next \( q \) queries is described as follows:
- The first line of a query contains an integer \( m \), the number of authorized settlements for that year.
- The second line contains \( m \) distinct integers which are the settlement numbers chosen as temporary assembly halls.
Note: It is guaranteed that the given roads form a tree.
outputFormat
For each query, output one line containing \( m \) integers. The \( i \)th integer should represent the number of races governed by the \( i \)th authorized settlement (in the order given in the query input).
sample
5
1 2
1 3
3 4
3 5
2
2
1 4
1
3
4 1
5
</p>