Restore Peace

Hard
0/120
Average time to solve is 50m
profile
Contributed by
2 upvotes
Asked in companies
OlaAmazon

Problem statement

There is a kingdom with ‘N’ cities that are connected by roads. The cities are connected to form a tree, where cities represent the nodes, and the roads represent the edges. The cities are numbered from 0 to ‘N-1’ and the length of each road is 1 unit.

A tree is a hierarchical data structure defined as a collection of nodes.

Now, a battle is going on in some cities of the kingdom, where the king must visit to restore peace. Initially, the king is at the 0th node(root node). The cities where battles are going on will be given as an input array, and the king has to visit in the same order as given in the input array 'battles'. You have to find the total length of the road the king has to travel to restore peace in all the cities.

For Example:
For the test case:
N = 3, Q = 2
Edge 1 connects 0 and 1
Edge 2 connects 0 and 2
battles = [1, 2]

The king has to visit first city 1 and then city 2. To visit city 1, he has to travel 1 unit, and now he is in city 1. To visit city 2 from city 1, he has to travel another 2 units. So overall, he has to travel 3 units of distance.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line will contain a single integer ‘T’ denoting the number of test cases. Then ‘T’ test cases follow.

The first line of each test case will contain two space-separated integers, ‘N’ and ‘Q’, denoting the total number of cities and the number of cities the king has to visit, respectively.

The next ‘N - 1’ line of each test case will contain two space-separated integers U[i] and V[i] denoting the two cities each road connects.

The next line of each test case will contain ‘Q’ space-separated integers denoting the cities where the king has to visit.
Output Format :
For each test case, print a single integer denoting the total length of the road the king has to travel to restore peace in all the cities.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function. 
Constraints:
1 <= T <= 10
1 <= Q <= N <= 10^5
0 <= U[i], V[i] <= N - 1

It is guaranteed that the sum of ‘N’ and sum of ‘Q’ over all test cases doesn’t exceed 10^5.

Time Limit: 1 sec.   
Sample Input 1:
2
4 2
0 1
0 2
1 3
1 3
5 4
0 1
1 2
2 3
3 4
1 2 3 4
Sample Output 1:
2
4
Explanation For Sample Output 1:
For the first test case, to reach city 1, the king has to travel 1 unit, and to visit city 3, he has to visit another 1 unit. So, he has to travel 2 units.
The path would be 0 -> 1 -> 3

For the second test case, to reach city 1, the king has to travel 1 unit, then to visit city 2, the king has to travel another 1 unit, then to visit city 3, he has to travel another 1 unit and to visit city 4 hr has to travel another 1 unit. So he has to travel 4 units.
The path would be 0 -> 1 -> 2 -> 3 -> 4
Sample Input 2:
2
5 4
0 1
0 2
0 3
0 4
1 0 4 2
8 4
0 1
1 2
1 4
0 3
3 7
3 6
6 5
5 7 6 2
Sample Output 2:
5
12
Hint

Find the distance between the two positions each time separately.

Approaches (2)
Breadth-First Search

We will iterate over the battle cities in the given order and calculate the distance between the current position of the king and his next position each time separately.

To calculate the distance between two cities, we will use the breadth-first search algorithm.

 

Algorithm:

 

  • bfs function:
    • This function will take the current city, “cur” and the next city, “tar” of the king, and the graph as arguments and will return the distance between these two cities.
    • Declare two arrays - “vis” and “dist” to store whether a city is visited or not and to store the distance of each city from the “cur” city, respectively.
    • Declare a queue of integers, and push the “cur” city to it.
    • Set the distance of the “cur” city as 0.
    • While ‘q’ is not empty.
      • Take out the front element of the ‘q’.
      • If this is the “tar” city, return the distance of this city from the “dist” array.
      • Iterate over the adjacent cities.
        • If the city is not visited
          • Mark it as visited
          • Push it to the queue
          • Update the “dist” array.

 

  • given function:
    • Create the graph from the given input.
    • Declare two variables - “ans” to store the answer and “cur” to store the current city of the king.
    • Iterate over the battle cities.
      • Declare a visited array “vis”.
      • Calculate the distance between the current city and the next city using the “bfs” function. Add this to the “ans”.
      • Update the “cur” as the next city.
    • Return the “ans”.
Time Complexity

O(Q * N), where  ‘N’ and ‘Q’ represent the total number of cities and the number of cities the king has to visit, respectively.

 

Since we are iterating over the battles array and in each iteration we are calling the “bfs” function, which has a time complexity of O(N), the overall time complexity will be O(Q * N).

Space Complexity

O(N), where ‘N’ represents the total number of cities in the kingdom.

 

Since we are creating a graph and the visited array and queue in the “bfs” function, each having a size of ‘N’, the total time complexity will be O(N).

Code Solution
(100% EXP penalty)
Restore Peace
Full screen
Console