#C6781. Tree Diameter Calculation
Tree Diameter Calculation
Tree Diameter Calculation
You are given a weighted tree with n nodes, where the tree is an undirected connected graph without cycles. Each edge has an associated weight. The task is to compute the diameter of the tree, which is defined as the maximum distance between any two vertices.
Mathematically, the diameter can be expressed as: $$\text{diameter} = \max_{u,v \in V} d(u,v)$$ where $$d(u,v)$$ is the sum of weights of the edges on the unique path between nodes \(u\) and \(v\).
The input is read from the standard input (stdin
) and the result (the diameter) must be printed to the standard output (stdout
).
inputFormat
The first line contains an integer n
(the number of nodes). If n > 1
, each of the next n-1
lines contains three space-separated integers u
, v
, and w
representing an edge between nodes u
and v
with weight w
. If n == 1
, there are no subsequent edge lines.
outputFormat
Print a single integer representing the diameter of the tree.
## sample1
0
</p>