Problem Statement
Manao is working in the Tree Research Center.
It may come as a surprise that the trees they research are not the ones you can see in a park.
Instead, they are researching special graphs.
(See Notes for definitions of terms related to these trees.)
Manao's daily job is reconstructing trees, given some partial information about them. Today Manao faced a difficult task. He needed to find the maximum possible diameter of a tree, given the following information:
Manao's daily job is reconstructing trees, given some partial information about them. Today Manao faced a difficult task. He needed to find the maximum possible diameter of a tree, given the following information:
- Some vertex in the tree is called V.
- The distance between V and the farthest vertex from V is D.
- For each x between 1 and D, inclusive, Manao knows the number of vertices such that their distance from V is x.
Definition
- Class:
- TheTree
- Method:
- maximumDiameter
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int maximumDiameter(int[] cnt)
- (be sure your method is public)
Notes
- A tree is a connected graph with no cycles. Note that each tree with N vertices has precisely N-1 edges.
- The distance between two vertices of a tree is the smallest number of edges one has to traverse in order to get from one of the vertices to the other one.
- The diameter of a tree is the maximum of all pairwise distances. In other words, the diameter is the distance between the two vertices that are the farthest away from each other.
Constraints
- cnt will contain between 1 and 50 elements, inclusive.
- Each element of cnt will be between 1 and 1000, inclusive.
Examples
{3}Returns: 2
The only tree that matches the given information is shown below. The vertex V is red.{2, 2}Returns: 4
There are two trees which correspond to the given partial information: The tree on the left has diameter 3 and the tree on the right has diameter 4.{4, 1, 2, 4}Returns: 5
This is one example of a tree that corresponds to the given constraints and has the largest possible diameter.{4, 2, 1, 3, 2, 5, 7, 2, 4, 5, 2, 3, 1, 13, 6}Returns: 21