#K16056. Maximum Distance in a Tree
Maximum Distance in a Tree
Maximum Distance in a Tree
You are given a tree with n vertices (numbered from 1 to n) and n-1 edges. Each edge connects two vertices with a given positive weight. The tree is undirected and connected.
Your task is to compute the maximum distance between any pair of vertices in the tree. In other words, find the diameter of the tree.
It is known that the diameter of a tree can be computed by performing two breadth-first searches (BFS). First, perform BFS from an arbitrary vertex to find the farthest vertex u. Then, perform BFS from u to find the farthest vertex v and the distance between them. Mathematically, if we denote the distance between vertices u and v as \(d(u,v)\), the answer is \(\max_{u,v \in T} d(u,v)\).
inputFormat
The input is given via stdin and has the following format:
- The first line contains an integer n (\(1 \leq n \leq 10^5\)), representing the number of vertices.
- The next n-1 lines each contain three integers u, v and w (\(1 \leq u,v \leq n\); \(1 \leq w \leq 10^6\)), denoting an edge between vertex u and vertex v with weight w.
It is guaranteed that the given edges form a tree.
outputFormat
Output a single integer to stdout that represents the maximum distance between any two vertices of the tree.
## sample4
1 2 1
2 3 2
2 4 3
5
</p>