atcoder#AGC018D. [AGC018D] Tree and Hamilton Path
[AGC018D] Tree and Hamilton Path
Score : points
Problem Statement
There is a tree with vertices, numbered through . The -th edge in this tree connects Vertices and and has a length of .
Joisino created a complete graph with vertices. The length of the edge connecting Vertices and in this graph, is equal to the shortest distance between Vertices and in the tree above.
Joisino would like to know the length of the longest Hamiltonian path (see Notes) in this complete graph. Find the length of that path.
Notes
A Hamiltonian path in a graph is a path in the graph that visits each vertex exactly once.
Constraints
- The given graph is a tree.
- All input values are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the length of the longest Hamiltonian path in the complete graph created by Joisino.
5
1 2 5
3 4 7
2 3 3
2 5 2
38
The length of the Hamiltonian path → → → → is . Since there is no Hamiltonian path with length or greater in the graph, the answer is .
8
2 8 8
1 5 1
4 8 2
2 5 4
3 8 6
6 8 9
2 7 12
132