Minimum distance
Practice
2 (1 votes)
Algorithms
Approved
Graphs
Hard
Problem
86% Success 582 Attempts 50 Points 2s Time Limit 512MB Memory 1024 KB Max Code

You are given a tree and q queries. Each query consists of \(k_i\) vertices: \(v_{i,1}\), ..., \(v_{i,k}\).

Let \(f_i(u)\) be the minimum between distances from u to each \(v_{i,j}\), for \(1 \le j \le k_i\). For each query you have to find value of \(\max\limits_{u \in V}f_i(u)\).

Input Format:
First line of input contains n and q (\(2 \leq n \leq 2 \cdot 10^5\); \(1 \leq q \leq 10^5\)).

Then follow \(n - 1\) lines with edges descriptions. The i-th of them contains integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n\)) — describing the edge connecting vertices \(a_i\) and \(b_i\).

Then follow q lines with query descriptions. The i-th of them contains integer \(k_i\) (\(1 \leq k_i \leq 5\)) followed by \(k_i\) integers \(v_{i, j}\) (\(1 \leq v_{i, j} \leq n\)).

Note that \(v_{i,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 \(f_i(u)\) among all vertices in the tree.

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

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
8 votes
Tags:
AlgorithmsDepth First SearchC++Graphs
Points:50
1 votes
Tags:
AlgorithmsApprovedGraphsHardOpen
Points:50
2 votes
Tags:
Depth First SearchAlgorithmsGraphs