Minimum distance

2

1 votes
Algorithms, Approved, Graphs, Hard
Problem

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

Let fi(u) be the minimum between distances from u to each vi,j, for 1jki. For each query you have to find value of maxuVfi(u).

Input Format:
First line of input contains n and q (2n2105; 1q105).

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

Then follow q lines with query descriptions. The i-th of them contains integer ki (1ki5) followed by ki integers vi,j (1vi,jn).

Note that vi,j 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 fi(u) among all vertices in the tree.

Time Limit: 2
Memory Limit: 512
Source Limit:
Explanation
  • In the first query: f(1)=0, f(2)=1, f(3)=1, f(4)=2, f(5)=2
  • In the second query: f(1)=0, f(2)=0, f(3)=1, f(4)=1, f(5)=1
Editor Image

?