#P1774E. Two Chess Pieces
Two Chess Pieces
No submission language available for this problem.
Description
Cirno_9baka has a tree with nodes. He is willing to share it with you, which means you can operate on it.
Initially, there are two chess pieces on the node of the tree. In one step, you can choose any piece, and move it to the neighboring node. You are also given an integer . You need to ensure that the distance between the two pieces doesn't ever exceed .
Each of these two pieces has a sequence of nodes which they need to pass in any order, and eventually, they have to return to the root. As a curious boy, he wants to know the minimum steps you need to take.
The first line contains two integers and ().
The -th of the following lines contains two integers , denoting the edge between the nodes of the tree.
It's guaranteed that these edges form a tree.
The next line contains an integer () and integers (, all are distinct) — the sequence of nodes that the first piece needs to pass.
The second line contains an integer () and integers (, all are distinct) — the sequence of nodes that the second piece needs to pass.
Output a single integer — the minimum steps you need to take.
Input
The first line contains two integers and ().
The -th of the following lines contains two integers , denoting the edge between the nodes of the tree.
It's guaranteed that these edges form a tree.
The next line contains an integer () and integers (, all are distinct) — the sequence of nodes that the first piece needs to pass.
The second line contains an integer () and integers (, all are distinct) — the sequence of nodes that the second piece needs to pass.
Output
Output a single integer — the minimum steps you need to take.
Note
In the first sample, here is one possible sequence of steps of length .
The second piece moves by the route .
Then, the first piece moves by the route .
In the second sample, here is one possible sequence of steps of length :
The first piece moves by the route .
Then, the second piece moves by the route .
Then, the first piece moves by the route .
Then, the second piece moves by the route .