Egalitarianism3
SRM 630 · 2014-07-26 · by cgy4ever
Problem Statement
You are given the
The distance between two cities is the sum of lengths of roads on the sequence of roads that connects them. (Note that this sequence of roads is always unique.)
You want to select k cities in such a way that all pairwise distances between the selected cities are the same. In other words, there must be a distance d such that the distance between every two selected cities is d. Return the largest possible value of k for which this is possible.
Constraints
- n will be between 1 and 50, inclusive.
- a will contain exactly n-1 elements.
- b will contain exactly n-1 elements.
- len will contain exactly n-1 elements.
- Each element in a will be between 1 and n, inclusive.
- Each element in b will be between 1 and n, inclusive.
- Each element in len will be between 1 and 1,000, inclusive.
- The graph described by a and b will be a tree.
4
{1,1,1}
{2,3,4}
{1,1,1}
Returns: 3
There are 4 cities and 3 roads, each of length 1. The roads connect the following pairs of cities: (1,2), (1,3), and (1,4). The optimal answer is k=3. We can select three cities in the required way: we select the cities {2, 3, 4}. The distance between any two of these cities is 2.
6
{1,2,3,2,3}
{2,3,4,5,6}
{2,1,3,2,3}
Returns: 3
Again, the largest possible k is 3. There are two ways to select three equidistant cities: {1, 4, 6} and {4, 5, 6}. (In both cases the common distance is 6.)
10
{1,1,1,1,1,1,1,1,1}
{2,3,4,5,6,7,8,9,10}
{1000,1000,1000,1000,1000,1000,1000,1000,1000}
Returns: 9
1
{}
{}
{}
Returns: 1
Note that n can be 1.
2
{1}
{2}
{3}
Returns: 2
Submissions are judged against all 172 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Egalitarianism3 with a public method int maxCities(int n, vector<int> a, vector<int> b, vector<int> len) · 172 test cases · 2 s / 256 MB per case