Cyclic Distance
Time limit3sMemory limit512 MB
Choose k distinct vertices of a weighted tree to maximize the total tree distance along the cycle p1 to pk and back to p1, and output that maximum.
- Level
Hard9 of 10
- Topics
- Tree, Dynamic programming, Binary search
- Solved
- No attempts yet
Problem
The technique from the problem 'Alien' turns a naive O(nk) dynamic programming solution into O(n log n). More problems that use this technique would make contests better, like the two unsolved problems in 300iq Contest 2.
You are given a weighted tree with vertices and edges. The -th edge connects vertices and and has length . Let be the distance between vertices and , which is the sum of the weights on the simple path between them.
Choose distinct vertices that maximize . Output the maximum sum.
Input
The first line contains an integer (), the number of test cases. Each test case is given as follows.
The first line contains two integers and (, ).
Each of the following lines contains three integers , , and (, ).
It is guaranteed that .
Output
For each test case, output one line containing one integer: the answer.