Under construction

Still being built. Things may change

Phonomenal Reviews

Interview

Time limit2sMemory limit512 MB

Summary
Given a tree and a set of M marked nodes, find the minimum number of edges Jo must walk to visit every marked node, starting anywhere.
Level

Medium6 of 10

Topics
Tree, DFS, Graph, Greedy
Solved
No attempts yet

Problem

Jo is a blogger who reviews restaurants. Today she wants to visit every Vietnamese pho restaurant in the Waterloo area and decide which one is best.

Waterloo has NN restaurants, numbered 00 to N−1N-1. Only MM of them are pho restaurants. Jo may start at any restaurant. Waterloo has N−1N-1 roads, and each road connects exactly two restaurants. Using only these roads, Jo can travel from any restaurant to any other restaurant. Travelling along one road takes exactly 1 minute.

In computer science a road network with this structure is called a tree. Here are three examples of trees.

Every tree has this property. Between any two points there is exactly one path that repeats no road.

What is the least amount of time Jo has to spend travelling on roads to visit all of the pho restaurants?

Input

The first line contains two integers NN and MM (2≤M≤N≤1000002 \le M \le N \le 100000).

The second line contains MM distinct integers, the numbers of the pho restaurants.

Each of the next N−1N-1 lines contains two integers. The aia_i and bib_i on line ii (0≤ai,bi≤N−10 \le a_i, b_i \le N-1) describe a road between restaurant aia_i and restaurant bib_i.

Output

Print one line with one integer, the minimum number of minutes Jo has to spend travelling on roads in order to visit every pho restaurant.

Examples2

  1. Example 1

    Input
    8 2
    5 2
    0 1
    0 2
    2 3
    4 3
    6 1
    1 5
    7 3
    
    Expected output
    3
    
  2. Example 2

    Input
    8 5
    0 6 4 3 7
    0 1
    0 2
    2 3
    4 3
    6 1
    1 5
    7 3
    
    Expected output
    7