Get Time

   Problem Statement  

 Problem Statement for FoxMeeting

Problem Statement


Foxland is a country with n cities. The cities are numbered 1 through n. The road network in Foxland is a tree. You are given the description of the roads: three int[]s A, B and L with n-1 elements each. For each valid i, there is a bidirectional road connecting the cities A[i] and B[i], and the length of the road is L[i].

There are some foxes in Foxland. Each fox lives in a different city. You are given a int[] foxes. The elements of foxes are the numbers of cities inhabited by foxes.

The foxes are running a secret candy-smuggling network. Recently they have been harrassed by a new strict police officer: cat Taro. In order to avoid him, the foxes have now learned to communicate by waving their tails at each other. However, two foxes can only communicate in this way if they see each other. More precisely, they must either both be in the same city, or they have to be in two adjacent cities. (Adjacent cities are cities directly connected by a road, regardless of the road's length.)

Foxes can forward messages to other foxes, which makes communication transitive: If fox x can talk to fox y and fox y can talk to fox z, fox x will be able to communicate with fox z as well.

In order to avoid Taro, each fox must be able to talk to all other foxes (either directly or indirectly). If it isn't possible in their current configuration, some foxes may be forced to move into different cities. The foxes think that it is also suspicious if a fox moves too far from its current home. Therefore, they want to minimize the maximum distance traveled by any fox.

Formally, find and return the smallest integer D with the following property: It is possible to relocate some (possibly zero, possibly all) of the foxes into new cities in such a way that:

  • For each fox, the total total distance traveled by that fox is at most D.
  • After the relocation, all foxes can communicate (either directly or indirectly).



Parameters:int[], int[], int[], int[]
Method signature:int maxDistance(int[] A, int[] B, int[] L, int[] foxes)
(be sure your method is public)


-n will be between 1 and 50, inclusive.
-A, B, and L will contain exactly n-1 elements each.
-Each element in A and B will be between 1 and n, inclusive.
-Each element in L will be between 1 and 100,000, inclusive.
-A, B, and L will define a tree.
-Number of elements in foxes will be between 1 and n, inclusive.
-Each element in foxes will be between 1 and n, inclusive.
-All the elements of foxes are distinct.


{1, 2}
Returns: 0
There are two cities connected by a single road of length 5. There are two foxes: one in each city. As they are in adjacent cities, they can already communicate so no relocation is necessary.
{1, 2}
{2, 3}
{1, 1}
{1, 3}
Returns: 1
This country has three cities and two roads: 1-2 (length 1) and 2-3 (length 1). Initially, there is one fox in city 1 and another fox in city 3. In one optimal solution, the first fox will move from city 1 to city 2. In another optimal solution, both foxes will move from their respective cities to city 2. In each of the solutions described above we have D = 1.
{1, 2}
{2, 3}
{1, 1}
{1, 2, 3}
Returns: 0
No relocation is necessary, all foxes can already talk to each other.
Returns: 0
Returns: 107013
Returns: 89342

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

This problem was used for:
       2015 TopCoder Open Algorithm Round 2A - Division I, Level Two