Minimum distance

2

1 votes
Algorithms, Approved, Graphs, Hard
Problem

You are given a tree and q queries. Each query consists of vertices: , ..., .

Let be the minimum between distances from u to each , for . For each query you have to find value of .

Input Format:
First line of input contains n and q (; ).

Then follow lines with edges descriptions. The i-th of them contains integers and () — describing the edge connecting vertices and .

Then follow q lines with query descriptions. The i-th of them contains integer () followed by integers ().

Note that are not necessarily distinct in a single query.

Output Format:
Print q lines. The i-th of them should contain a single integer — maximum of among all vertices in the tree.

Sample Input
5 2
1 2
1 3
2 4
2 5
1 1
2 1 2
Sample Output
2
1
Time Limit: 2
Memory Limit: 512
Source Limit:
Explanation
  • In the first query: , , , ,
  • In the second query: , , , ,
Editor Image

?