This page is still under construction.

Parts of this page are still being built. What you see may change.

Hidden Maze

Time limit2sMemory limit256 MB

Summary
Compute the expected median edge weight over all tree node pairs at odd distance, and print it as a reduced fraction.
Level

Hard9 of 10

Topics
Divide and conquer, Tree, Sorting, Probability
Solved
No attempts yet

Problem

Helen and Henry are fans of the TV show "Hidden Maze", which is very popular in Hiddenland. In the show two participants, usually a married couple, run through a maze of nn halls connected by tunnels. Each tunnel joins two different halls, and no two halls are joined by more than one tunnel.

At the start of the show the two participants are placed in two different halls. They have to meet before the time runs out. To pass through a tunnel, a participant has to find the clue of that tunnel, a positive integer written on a small piece of paper.

The two win if they meet inside a tunnel before the time runs out and also find the clue of the tunnel where they met. The prize is decided by sorting every clue the two of them found and taking the median. The game is always arranged so that the number of clues they find is odd.

The maze never changes between episodes, and Helen and Henry drew a complete map of it. If every tunnel is visited at most once, there is exactly one path between any two halls.

Hillary, who worked for the company that built the maze, said in an interview that the maze was created by this randomized algorithm:

  1. Pick the number of halls nn. Build nn halls numbered from 11 to nn.
  2. Choose two integers ii and jj at random, each of them uniformly distributed between 11 and nn.
  3. If halls ii and jj are the same, or are already connected by a path of tunnels, go back to step 2.
  4. Build the tunnel between ii and jj. If there is now a path of tunnels between every two halls, stop, otherwise go back to step 2.

Each tunnel holds exactly one clue and its value never changes between episodes. Helen and Henry wrote the value of the clue of every tunnel on their map.

Finding a clue and running through the tunnel to the next hall takes 1 minute. Running from a hall to the middle of a tunnel takes half a minute, and the two meet in the middle of a tunnel at the end. The time given is only enough to meet if both act optimally: they run towards each other along the shortest path, they never fail to find a clue, and they never turn into a tunnel that is not on the shortest path. The clues they find are therefore exactly the clues of the tunnels on the shortest path between their starting halls, including the clue of the tunnel where they meet. To make them meet in the middle of a tunnel, the length of the shortest path between the two starting halls is always odd.

The pair of starting halls is selected uniformly from all pairs whose shortest path has odd length. Find the expected value of the prize the two of them win.

Input

The first line contains one integer nn (2≤n≤300002 \le n \le 30000), the number of halls. Each of the next n−1n - 1 lines contains three integers uiu_i, viv_i, cic_i (1≤ui,vi≤n1 \le u_i, v_i \le n, 1≤ci≤1061 \le c_i \le 10^6), describing the ii-th tunnel: it connects halls uiu_i and viv_i and holds the clue with value cic_i. The maze is always created by the randomized algorithm given in the statement.

Output

Print the expected value of the prize on one line as an irreducible fraction p/q, where pp and qq are integers, q≥1q \ge 1 and gcd⁡(p,q)=1\gcd(p, q) = 1. Print the fraction in the form p/q even when q=1q = 1.

Note

The shortest path between the two starting halls holds an odd number of tunnels, so the median is a single well defined value. Clues of the same value may sit in several tunnels, and that does not change the median.

Examples3

  1. Example 1

    Input
    2
    2 1 1
    
    Expected output
    1/1
    
  2. Example 2

    Input
    5
    2 4 4
    1 2 5
    5 4 2
    5 3 3
    
    Expected output
    7/2
    
  3. Example 3

    Input
    5
    4 1 2
    5 3 2
    4 2 3
    5 4 7
    
    Expected output
    19/6