Help Chef Solution | CodeChef Problem Solution 2022
Chef is the king of the kingdom named Chefland. Chefland consists of cities numbered from to and roads in such a way that there exists a path between any pair of cities. In other words, Chefland has a tree like structure.
Out of the cities in Chefland, are considered tourist attractions.
Chef decided to visit exactly out of tourist attractions. He wishes to stay in the city such that the sum of distances of the nearest tourist attractions from city is as minimum as possible.
More formally, if Chef stays at city and visits tourist attractions denoted by set , then, the value should be minimum.
Here, denotes the distance between cities and which is given by the number of roads between cities and .
Find the city in which Chef should stay. If there are multiple such cities, print the one with the largest number.
Input Format
- First line will contain integer , the number of test cases. Then the test cases follow.
- The first line of each test case contains two integers and denoting the number of total cities and the number of tourist attractions in Chefland.
- The second line of each test case contains integers denoting the numbers of cities which are tourist attractions in Chefland.
- The next lines contain two integers each, and denoting that a road exists between cities and .
Output Format
- For each test case, output in a single integer the number of the city in which Chef should stay so that sum of distances to nearest tourist attractions is as minimum as possible.
- If there are multiple such cities, output the city with the maximum number.
Constraints
Sample Input 1
2
5 3
2 4 5
1 2
1 3
3 4
3 5
5 2
2 5
1 2
1 3
3 4
3 5
Sample Output 1
5
5
Explanation
Test case : Chef needs to visit tourist attractions. The total distance to visit tourist attractions from each of the cities is:
- From city : Nearest tourist attractions are cities and or . The sum of distances for these cities is .
- From city : Nearest tourist attractions are cities and or . The sum of distances for these cities is .
- From city : Nearest tourist attractions are cities and . The sum of distances for these cities is .
- From city : Nearest tourist attractions are cities and . The sum of distances for these cities is .
- From city : Nearest tourist attractions are cities and . The sum of distances for these cities is .
The cities from which the distance is minimum are and . Out of these, is the highest.
Test case : Chef needs to visit tourist attractions. The total distance to visit tourist attractions from each of the cities is:
- From city : Nearest tourist attraction is city . The sum of distances is .
- From city : Nearest tourist attraction is city . The sum of distances is .
- From city : Nearest tourist attraction is city . The sum of distances is .
- From city : Nearest tourist attraction is city . The sum of distances is .
- From city : Nearest tourist attraction is city . The sum of distances is .
The cities from which the distance is minimum are and . Out of these, is the highest.
No comments:
Post a Comment