Under construction

Still being built. Things may change

The Guardian of Balance

Interview

Time limit1.5sMemory limit1024 MB

Summary
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 NN vertices. To balance the tree, Kyeongin picks a root as follows.

  • Let ViV_i be the variance of the distances from vertex ii to all vertices. Pick the vertex with the smallest ViV_i 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 NN. (1≤N≤200 000)(1 \le N \le 200\,000)

Each of the next N−1N-1 lines contains three integers uu, vv, and ww separated by spaces. This means there is an edge of length ww between vertices uu and vv. (1≤u,v≤N;u≠v;1≤w≤10 000)(1 \le u, v \le N; u \neq v; 1 \le w \le 10\,000)

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: E[X]=1∣X∣∑x∈Xx\mathrm{E}[X]=\frac{1}{|X|}\sum\limits_{x \in X}x
  • Variance: Var[X]=E[(X−E[X])2]=1∣X∣∑x∈X(x−E[X])2\mathrm{Var}[X]=\mathrm{E}[(X-\mathrm{E}[X])^2]=\frac{1}{|X|}\sum\limits_{x \in X}(x-\mathrm{E}[X])^2

Examples1

  1. Example 1

    Input
    4
    1 2 8
    1 3 8
    3 4 9
    
    Expected output
    3