Circuit Resistance
InterviewTime limit1sMemory limit512 MB
Given a tree whose edges carry resistances, find the maximum and minimum path resistance between any two leaf nodes.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Graph, Dynamic programming
- Solved
- No attempts yet
Problem
A PCB (Printed Circuit Board) has nodes, and resistors connect them without any cycle.
Given the information about the PCB, find the largest and smallest resistance values that can be obtained by connecting two edge nodes. An edge node is a node with exactly one resistor connected to it.

For the figure above, finding the largest and smallest resistance values that connect two edge nodes gives the figures below.

The figure above shows in red the part containing the resistors used to make the largest resistance value connecting two edge nodes. The largest resistance value obtained in this circuit is .

The figure above shows the part containing the resistors used to make the smallest resistance value connecting two edge nodes. The smallest resistance value obtained in this circuit is .
Write a program that finds the maximum and minimum resistance obtainable by connecting two edge nodes on the PCB.
Input
The first line gives the number of nodes as an integer.
The next lines each give three integers , , and , separated by spaces, as edge information.
and are the numbers of the nodes the resistor connects, and is the resistance value. (The unit is .)
Output
Print the largest and smallest resistance values connecting two edge nodes, one per line in that order.
Hint
The resistance values in the figures differ from the actual ones.