( Problem E ) Pikachu and Champions League
Practice
4.5 (2 votes)
Algorithms
Depth first search
Graphs
Medium
Problem
80% Success 501 Attempts 30 Points 4s Time Limit 512MB Memory 1024 KB Max Code

Pikachu has already defeated so many legendary Pokemon, and is now organizing the Champions League where champions of all Conferences are going to battle for glory.

There are $$ N $$ Conferences around the world, connected by exactly $$ N-1 $$ bidirectional roads. It is always possible to reach one Conference from another.

To choose the Champion of Champions, Pikachu chooses a set $$ S $$ of Conferences. Now, each Conference champion in $$ S $$ battles with every other Conference champion in $$S$$ and for each battle, one of the champion has to travel. Pikachu wants to know the maximum distance any champion would travel to battle.

Pikachu has thought of $$ Q $$ such sets. He wants to know the maximum distance for each set so that he can choose the least tiresome set.

Constraints:

  • \(2\le N\le 500000\)
  • \(1\le Q\le 200000\)
  • It is always possible to reach one Conference from another
  • \(2\le |S|\le 500000\)
  • Sum of \(|S|\) over all Q sets is at most \(500000\)
  • All elements in S are distinct

Input format:

  • First line contains two space separated integers $$ N $$ and $$ Q $$
  • Next $$ N-1 $$ lines contain two space separated integers, $$u $$ and $$ v $$ (\(1\le u,v\le N\)), such that there is a path of length 1 between $$ u $$ and $$ v $$
  • Next $$Q$$ lines each starts with integer $$ K $$, the number of elements in set followed by $$ K $$ distinct elements.

Output format:

  • Output $$ Q $$ lines, each of which contains maximum distance for the set of Conferences.

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:30
3 votes
Tags:
AlgorithmsApprovedGraphsMedium
Points:30
152 votes
Tags:
ApprovedDepth First SearchGraphsMediumOpen
Points:30
6 votes
Tags:
AlgorithmsDepth First SearchGraphsMedium