Tree with the longest diameter
InterviewTime limit2sMemory limit512 MB
Given the number of vertices at each level from the root, build a tree realizing those level counts with the maximum possible diameter.
- Level
Medium5 of 10
- Topics
- Tree, Greedy, Implementation, Graph
- Solved
- No attempts yet
Problem
A tree is a connected graph with no cycle. A tree with vertices has edges.
The distance between two vertices is the smallest number of edges on a path from one vertex to the other. The diameter of a tree is the largest distance over all pairs of vertices.
Build a tree with the longest possible diameter under the conditions below.
- Call the root of the tree .
- Call the distance from to the farthest vertex .
- For every with , the number of vertices whose distance from is exactly is .
Given the array , print the largest diameter among the trees that satisfy the conditions.
Input
The first line has , the size of the array ().
The second line has the values through in order. ()
Output
Print on the first line the diameter of the tree with the largest diameter among the trees that satisfy the conditions.