Tree Trunk and Branches
InterviewTime limit2.5sMemory limit1024 MB
Given a rooted weighted tree, find the giga node (first node from the root with at least two children, or the single leaf), then output the trunk length and the longest branch length from the giga node to a leaf.
- Level
Medium5 of 10
- Topics
- Tree, DFS, Implementation, Recursion
- Solved
- No attempts yet
Problem

Micro, a civil servant at the city office, was ordered by his section chief to determine the length of the trunk and the length of the longest branch of a tree in the city.
Micro decided that using the tree data structure he learned at ICPC Sinchon Winter Algorithm Camp would make this task easier.

Micro defined the giga node to classify the trunk and branches of a tree.
The giga node is the first node with or more children when traversing from the root node. The name comes from shortening trunk and branch to giga. In the figure above, the giga node is node .

However, as in the figure above, when there is only leaf node, the leaf node is also the giga node.

Also, as in the figure above, the root node can also be the giga node.


- The trunk of the tree is from the root node to the giga node. In the figure above, the trunk is .
The length of the trunk is the sum of the edge lengths of the trunk, . - A branch of the tree is from the giga node to any leaf node. In the figure above, the branches are , , , , and , in total.
The length of a branch is the sum of the edge lengths of the branch. Fortunately, only the length of the longest branch needs to be recorded. The branch is the longest branch with an edge length sum of .
Micro transferred the city's tree into a tree data structure. But the section chief gave Micro another task! Let us measure the length of the trunk and the longest branch of the tree on behalf of the very busy Micro.
Input
The first line gives the number of nodes () and the number of the root node ().
After that, lines each give three integers , , (, ). This means that node and node are connected and the length of this edge is (). Nodes are numbered with integers from to , and the same edge is not given more than once.
Graphs that are not trees are not given as input.
Output
Print the length of the trunk of the tree and the length of the longest branch.