GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Thursday 5 May 2022

Vlad and Unfinished Business Codeforces Solution | Codeforces Problem Solution 2022

 F. Vlad and Unfinished Business
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vlad and Nastya live in a city consisting of n houses and n1 road. From each house, you can get to the other by moving only along the roads. That is, the city is a tree.

Vlad lives in a house with index x, and Nastya lives in a house with index y. Vlad decided to visit Nastya. However, he remembered that he had postponed for later k things that he has to do before coming to Nastya. To do the i-th thing, he needs to come to the ai-th house, things can be done in any order. In 1 minute, he can walk from one house to another if they are connected by a road.

Solution Click Below:- CLICK HERE

Vlad does not really like walking, so he is interested what is the minimum number of minutes he has to spend on the road to do all things and then come to Nastya. Houses a1,a2,,an he can visit in any order. He can visit any house multiple times (if he wants).

Input

The first line of input contains an integer t (1t104) — the number of input test cases. There is an empty line before each test case.

The first line of each test case contains two integers n and k (1kn2105) — the number of houses and things, respectively.

The second line of each test case contains two integers x and y (1x,yn) — indices of the houses where Vlad and Nastya live, respectively.

The third line of each test case contains k integers a1,a2,,ak (2ain) — indices of houses Vlad need to come to do things.

The following n1 lines contain description of city, each line contains two integers vj and uj (1uj,vjn) — indices of houses connected by road j.

It is guaranteed that the sum of n for all cases does not exceed 2105.

Output

Output t lines, each of which contains the answer to the corresponding test case of input. As an answer output single integer — the minimum number of minutes Vlad needs on the road to do all the things and come to Nastya.

Join Now for Solution:- 

No comments:

Post a Comment