A country consists of \(N\) cities that are represented as a tree graph. A tree graph is an undirected acyclic graph.
The leaders of the country decided to hold \(Q\) meetings. For the \(i^{th}\) meeting, there are \(K_i\) leaders in different cities. For each meeting, the leaders want to find a city such that the maximum distance between any of the \(K_i\) cities and a selected city is minimum.
Note: This city does not have to be a part of the \(K_i\) cities.
Your task is to determine the minimum of the maximum distances obtained between the \(K_i\) cities and the selected city.
Input format
- First line: Two space-separated integers \(N\) and \(Q\)
- Next \(N-1\) lines: Two space-separated integers \(u\) and \(v\) denoting an edge between \(u\) and \(v\)
- Next \(2 \times Q\) lines:
- First line: \(K_i\) denoting the number of nodes in the query
- Second line: \(K_i\) space-separated integers where \(X_i\) is denoting the cities of this query
Output format
Print \(Q\) lines, each line contains the value that represents the minimum of the maximum distance between all the \(K_i\) cities and the city selected.
Constraints
Queries as in order:
- The best city to choose is city 3
- The best city to choose is city 2
- The best city to choose is city 1
- The best city to choose is city 3
- The best city to choose is city 1
Here is a picture of the tree for further clarity:
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor