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.
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.
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.
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
2
4
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
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
5
12
Find the distance between the two positions each time separately.
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:
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).
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).