Under construction

Still being built. Things may change

Circuit Resistance

Interview

Time limit1sMemory limit512 MB

Summary
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 NN nodes, and N−1N-1 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 20Ω20 \Omega.

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 9Ω9 \Omega.

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 NN as an integer. (2≤N≤100 000)(2 \le N \le 100\,000)

The next N−1N-1 lines each give three integers pp, qq, and ww, separated by spaces, as edge information. (0≤p,q<N, p≠q, 1≤w≤10 000)(0 \le p, q < N,\ p ≠ q,\ 1 \le w \le 10\,000)

pp and qq are the numbers of the nodes the resistor connects, and ww is the resistance value. (The unit is Ω\Omega.)

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.

Examples1

  1. Example 1

    Input
    10
    0 1 2
    1 2 4
    2 3 2
    3 4 1
    2 7 7
    5 6 2
    6 7 1
    7 8 5
    8 9 2
    
    Expected output
    20
    9