Maximum sum of paths P36665


Statement
 

pdf   zip

You are given a tree with nn nodes, where each edge has a positive cost. Let xx and yy be any two adjacent nodes. Define p(x,y)p(x, y) as the maximum cost of all paths (with no repeated nodes) whose first step goes from xx to yy. Define c(x)c(x) as the sum of p(x,y)p(x, y) for all yy adjacent to xx. Please compute the maximum value of c(x)c(x) among all nodes xx.

Input

Input consists of several cases. Every case begins with the number of nodes nn, followed by n1n-1 edges, each one with two different nodes and the cost of the edge between them. Assume 2n1052 \le n \le 10^5. The nodes are numbered starting at zero. Each cost is an integer number between 1 and 1000. The given graph is always a tree. The number of steps between any two nodes is never larger than 1000.

Output

For every case, print the maximum c(x)c(x), and how many nodes xx achieve such a value.

Public test cases
  • Input

    2   0 1 100
    3   1 0 10  1 2 20
    4   1 0 10  1 2 20  3 1 30
    6   0 2 20  1 2 50  2 3 100  3 4 30  3 5 40
    

    Output

    100 2
    30 3
    60 1
    220 1
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++