#C515. Maximum Weight Path in a Tree
Maximum Weight Path in a Tree
Maximum Weight Path in a Tree
You are given a tree with N nodes and N-1 weighted edges. A tree is an undirected, connected acyclic graph. Your task is to compute the maximum weight of a simple path (i.e. a path that does not revisit any node) between any two distinct nodes.
The weight of a path is defined as the sum of the weights of all edges on that path. In mathematical notation, if a path passes through edges with weights \(w_1, w_2, \dots, w_k\), then its total weight is:
$$\sum_{i=1}^{k}w_i$$
This problem essentially asks you to determine the diameter of the tree where the distance is measured by the sum of edge weights.
Example:
Input: 4 1 2 3 2 3 4 2 4 2</p>Output: 7
In the above example, the maximum weight path has weight 7.
inputFormat
The first line of input contains an integer N representing the number of nodes in the tree. The following N-1 lines each contain three space-separated integers u, v, and w, which denote an undirected edge between nodes u and v with weight w.
All input is provided via standard input (stdin).
outputFormat
Output a single integer representing the maximum weight of a simple path in the tree. The result should be printed to standard output (stdout).
## sample4
1 2 3
2 3 4
2 4 2
7