Why the Mole Came Up to Jeongbo Island
InterviewTime limit2sMemory limit512 MB
On a weighted tree, sum the minimum edge weight over all unordered pairs of vertices.
- Level
Medium7 of 10
- Topics
- Tree, Union-find, Sorting, Greedy
- Solved
- No attempts yet
Problem
A mole has come up to Jeongbo Island!
The mole owns several burrows beneath Jeongbo Island. It likes to travel between them. Jeongbo Island has N mole burrows in total, connected by N-1 paths. Between any two burrows there is always exactly one path. That is, the burrows form a connected tree. Traveling along a path gives the mole W units of satisfaction.
One day, a mole whose burrows are arranged as in the figure below traveled from burrow 1 to burrow 4. The burrows it passes through are (1 → 2 → 3 → 4). On each trip, the mole gains the minimum satisfaction among those on the path. That is, for (1 → 4) it gains satisfaction 2, and for (6 → 2) it gains satisfaction 3.

The mole suddenly wondered what the total satisfaction would be after finishing trips for every pair of burrows (a, b). The pairs (a, b) and (b, a) count as the same path. That is, in the figure above it means the total satisfaction gained by traveling all of (1-2, 1-3, 1-4, 1-5, 1-6, 2-3, 2-4, 2-5, 2-6, 3-4, 3-5, 3-6, 4-5, 4-6, 5-6).
But the mole grew tired of calculating and fell fast asleep! Let us kindly calculate in place of the sleeping mole.
Input
The first line gives the number of burrows N. (1 ≤ N ≤ 100,000)
The following N-1 lines give X, Y, W. This means burrows X and Y are connected, and traveling this path gives satisfaction W. (1 ≤ X,Y ≤ N, 1 ≤ W ≤ 200)
Output
After finishing the trips described in the problem, output the total satisfaction the mole feels.