← 返回 meta 的题目列表Find Longest Path Between Two Nodes in a Tree
类型:online_judge
Given a tree, find the longest path between any two nodes.
Input:
The number of nodes in the tree, n (1 <= n <= 10^4)
A list of edges, each containing three integers u, v, w, indicating an edge with weight w between nodes u and v.
Output:
The length of the longest path.
Note: The tree is an undirected, acyclic graph.
Example:
Input:
5
1 2 3
1 3 4
2 4 5
2 5 6
Output:
15
In the example above, the longest path is 4 -> 2 -> 5 with a length of 15.
Example
Input
5
1 2 3
1 3 4
2 4 5
2 5 6