The Squirrel in the Tree
InterviewTime limit4sMemory limit1024 MB
Given a tree rooted at node 1 and a set of nodes holding nuts, find the shortest round trip from node 1 that visits every nut node.
Problem
A squirrel lives in a tree with nodes and edges. Each edge has length 1 and connects two nodes. The squirrel has hidden nuts in some of the nodes and now wants to collect them. If the squirrel starts at node 1 (the root of the tree), what is the shortest distance it must travel to fetch all the nuts and return to node 1? The squirrel can carry several nuts at the same time.
It is guaranteed that any pair of nodes can be reached by walking along a sequence of edges.
Input
The first line contains two integers and (), the number of nodes in the tree and the number of nodes with nuts. The second line contains distinct integers, the nodes where the squirrel has hidden nuts. Nodes are indexed 1 to . Then follow lines. Each line contains two integers , meaning there is an edge between node and node .
Output
Print one integer: the shortest distance the squirrel must travel to fetch all the nuts.