The Guardian of Balance
InterviewTime limit1.5sMemory limit1024 MB
Given a weighted tree, find the vertex whose distances to all vertices have the smallest variance, breaking ties by the smaller vertex number.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Kyeongin, the guardian of balance, is given a tree with vertices. To balance the tree, Kyeongin picks a root as follows.
- Let be the variance of the distances from vertex to all vertices. Pick the vertex with the smallest as the root. If several vertices tie, pick the one with the smallest number.
Find the root Kyeongin will pick.
Input
The first line contains the number of vertices .
Each of the next lines contains three integers , , and separated by spaces. This means there is an edge of length between vertices and .
Output
Print the root Kyeongin will pick.
Hint
Be careful that intermediate values do not become too small or too large during the calculation.
- Mean:
- Variance: