Farthest node in a weighted tree
InterviewTime limit1sMemory limit256 MB
For every node of a weighted tree with up to 50000 nodes, print its distance to the farthest node.
Problem
A weighted tree is a tree where every edge carries a number giving that edge's length. All lengths are positive.
For each node of the tree, find the largest distance from that node to any other node. The distance between two nodes is the sum of the edge lengths along the path that joins them.
Input
The first line contains the number of nodes . ()
Each of the next lines describes one edge with three positive integers. The first two are the labels of the nodes the edge joins (from to ), and the third is the length of the edge. The total length of all edges does not exceed . The input always describes a valid tree.
Output
Print exactly lines. The -th line contains the distance from node to the node farthest from it. ()