#P2052. [NOI2011] 道路修建

    ID: 994 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>树形结构2011NOI 系列广度优先搜索,BFS深度优先搜索,DFS

[NOI2011] 道路修建

Description

There are nn countries on planet W. To develop their economies, they decide to build bidirectional roads between countries so that all countries are connected. However, each king is stingy and they are only willing to build exactly n1n - 1 bidirectional roads.

Building each road costs an amount equal to the road length multiplied by the absolute difference between the numbers of countries on the two sides of this road (i.e., after removing this road, in the two resulting connected components). For example, in the figure below, the dashed road has 22 and 44 countries on its two sides. If its length is 11, then the cost is 1×24=21 \times |2 - 4| = 2. The numbers inside the circles in the figure represent country indices.

Because the number of countries is very large, there are many construction schemes, and the cost of each scheme is difficult to compute by hand. The kings decide to commission software that, for a given construction scheme, computes the required total cost. Please help design such software.

Input Format

The first line contains an integer nn, the number of countries on planet W. The countries are numbered from 11 to nn.

The next n1n - 1 lines describe the road network. The ii-th line contains three integers aia_i, bib_i, and cic_i, indicating that the ii-th bidirectional road is built between countries aia_i and bib_i, with length cic_i.

Output Format

Output a single integer, the total cost required to build all the roads.

6
1 2 1
1 3 1
1 4 2
6 3 1
5 2 1
20

Hint

Constraints: For 100%100\% of the testdata, 1ai,bin1 \leq a_i, b_i \leq n, 0ci1060 \leq c_i \leq 10^6, 2n1062 \leq n \leq 10^6.

Test point ID n=n=
11 22
22 1010
33 100100
44 200200
55 500500
66 600600
77 800800
88 10001000
99 10410^4
1010 2×1042 \times 10^4
1111 5×1045 \times 10^4
1212 6×1046 \times 10^4
1313 8×1048 \times 10^4
1414 10510^5
1515 6×1056 \times 10^5
1616 7×1057 \times 10^5
1717 8×1058 \times 10^5
1818 9×1059 \times 10^5
19,2019,20 10610^6

Translated by ChatGPT 5