Can anyone solve this?
Anonymous User
210
There are N cities in a country numbered 1 to N which are connected by N-1 roads (bidirectional or two-way). 
The length of each road is 1 and it is possible to reach any of the cities from any other city. 
Jack has H hideouts in different cities of the country. He is planning to put the booster in the roads so that he can get unimaginable speed 
and never run out of booster. He needs your help for this purpose. He is required to select the H/2 pairs in such a way that each hideout is 
part of only one pair and the distance to put the Booster is maximized. It means the total distance of putting the Booster is maximum possible.
Print the maximum booster distance possible.
Input:
8 4 
2 4 5 6
1 2
1 7
1 6
2 8
3 1
3 4
5 3
Output: 
6
Explanation ->
Number of cities, N = 8
Number of hideout, H = 4
4 hideouts are present in the city; 2, 4, 5 and 6.
For the given, we have to get 2 hideout pairs such that the total distance between them implementing a booster will be the maximum.
Pairs:
1. (2, 4): The route would be 2 - 1 - 3 - 4. The total distance would be 3.
2. (6, 5): The route would be 6 -1 - 3 - 5. The total distance would be 3.
The overall distance would be 3 + 3 = 6.
Note: The route 1 - 3 is common in between and is counted twice. Jack will take care of it on his own, you have to just tell him the
maximum booster distance which is 6 in this case. Also, it is possible to form different pairs but the maximum booster distance will remain 6.
Comments (0)