영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)
Problem
A squirrel lives in a tree with N nodes and N-1 edges. Each edge has a length of 1 and connects two nodes. In some of the nodes, the squirrel has hidden nuts, which he now wants to collect. If the squirrel starts at node 1 (the root of the tree), what is the shortest distance he needs to travel to collect all the nuts and return to node 1? The squirrel can carry multiple nuts at the same time.
It is guaranteed that it is possible to travel between all pairs of nodes via a sequence of edges.
Input
The first line contains two integers N and K (1 ≤ K ≤ N ≤ 10^5), the number of nodes in the tree and the number of nodes with nuts.
The second line contains K unique integers – the nodes where the squirrel has hidden nuts. The nodes are indexed from 1 to N.
Then follow N-1 lines. Each line contains two integers a,b (1 ≤ a, b ≤ N) which means that there is an edge between node a and node b.
Output
Print an integer: the shortest distance the squirrel needs to travel to collect all its nuts.
Points
Your solution will be tested on several test case groups. To get the points for a group, it must pass all the test cases in the group.
| Group | Point value | Constraints |
|---|---|---|
| 1 | 20 | The i:th edge connects node i and i+1 for all 1 ≤ i ≤ N-1 |
| 2 | 40 | 1 ≤ K ≤ N ≤ 1000 |
| 3 | 40 | No additional constraints. |
예제
예제 1
5 2 4 5 1 2 2 3 2 4 1 5
6
예제 2
10 4 5 4 8 6 2 7 1 2 4 2 2 5 5 3 5 10 6 7 8 4 9 4
12