Connection Status:
Competition Arena > TreeDiameters
SRM 703 · 2016-12-04 · by cgy4ever · Graph Theory
Class Name: TreeDiameters
Return Type: int
Method Name: getMax
Arg Types: (vector<int>)
Problem Statement

Problem Statement

This problem is about undirected trees with unit-length edges. In such a tree, the distance between two vertices x and y is the number of edges on the unique simple path that connects x and y.

Given a tree T, we can compute the distances between all pairs of its vertices. The largest of all those distances is called the diameter of the tree.

In a tree T, an unordered pair {x, y} of vertices is called an antipodal pair if their distance is equal to the diameter of the tree.

The value A(T) is defined as the number of distinct antipodal pairs in the tree T. Note that {a, b} is the same pair of vertices as {b, a}.

A subgraph of a graph G is a graph that can be obtained from G by erasing some (possibly none) vertices and some (possibly none) edges. A subtree of a tree T is a subgraph of T that is a tree. In other words, the subtrees of a tree T are precisely all connected subgraphs of T. Note that T is considered to be its own subtree.

You are given a int[] p with n elements. This int[] describes a tree T with n+1 vertices. The vertices are numbered 0 through n. For each valid i, there is an edge between the vertices p[i] and (i+1).

Consider all possible subtrees of the given tree T. For each such subtree T' that has at least two vertices, determine the value A(T'): the number of antipodal pairs in T'. Return the largest of all those values.

In other words, among all possible subtrees of the given tree T find the one that has the most antipodal pairs, and return their count.

Note that when counting antipodal pairs in a subtree we are looking for pairs of vertices with distance equal to the diameter of that particular subtree (which may be smaller than the diameter of the original tree).

Constraints

  • p will contain between 1 and 1,000 elements, inclusive.
  • For each i: 0 <= p[i] <= i.
Examples
0)
{0,1,2,2}
Returns: 3

This is a tree on 5 vertices, numbered 0 through 4. The tree contains the edges 0-1, 1-2, 2-3, and 2-4. The diameter of this tree is 3. There are two antipodal pairs: {0, 3} and {0, 4}. However, this is not the largest possible number of antipodal pairs. Consider the subtree that contains just the vertices {1, 2, 3, 4} and therefore only the edges 1-2, 2-3, and 2-4. The diameter of this smaller tree is 2. This smaller tree contains three antipodal pairs: {1, 3}, {1, 4}, and {3, 4}.

1)
{0,0,0,0}
Returns: 6

The given tree contains six antipodal pairs, which is more than any of its subtrees.

2)
{0,1,2,3,4,5,6,7}
Returns: 1

The entire tree is a single path. Any subtree of this tree will therefore also be a path. The diameter of a path is its length. Each path with a positive length contains exactly one antipodal pair: its endpoints.

3)
{0,0,0,2,3,4}
Returns: 3
4)
{0}
Returns: 1

Submissions are judged against all 106 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class TreeDiameters with a public method int getMax(vector<int> p) · 106 test cases · 2 s / 256 MB per case

Submitting as anonymous