루미 알고리즘

#1508플래티넘Squirrel In A Tree

PO 2021 결선코딩출제 Fredrik Ekholm영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

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.

GroupPoint valueConstraints
120The i:th edge connects node i and i+1 for all 1 ≤ i ≤ N-1
2401 ≤ K ≤ N ≤ 1000
340No 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

출처 표기

출처: Programmeringsolympiaden 2021년 결선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.