CF1774E.Two Chess Pieces
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Cirno_9baka has a tree with n 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 1 of the tree. In one step, you can choose any piece, and move it to the neighboring node. You are also given an integer d . You need to ensure that the distance between the two pieces doesn't ever exceed d .
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 n and d ( 2≤d≤n≤2⋅105 ).
The i -th of the following n−1 lines contains two integers ui,vi (1≤ui,vi≤n) , denoting the edge between the nodes ui,vi of the tree.
It's guaranteed that these edges form a tree.
The next line contains an integer m1 ( 1≤m1≤n ) and m1 integers a1,a2,…,am1 ( 1≤ai≤n , all ai are distinct) — the sequence of nodes that the first piece needs to pass.
The second line contains an integer m2 ( 1≤m2≤n ) and m2 integers b1,b2,…,bm2 ( 1≤bi≤n , all bi are distinct) — the sequence of nodes that the second piece needs to pass.
输出格式
Output a single integer — the minimum steps you need to take.
输入输出样例
输入#1
4 2 1 2 1 3 2 4 1 3 1 4
输出#1
6
输入#2
4 2 1 2 2 3 3 4 4 1 2 3 4 1 1
输出#2
8
说明/提示
In the first sample, here is one possible sequence of steps of length 6 .
- The second piece moves by the route 1→2→4→2→1 .
- Then, the first piece moves by the route 1→3→1 .
In the second sample, here is one possible sequence of steps of length 8 :
- The first piece moves by the route 1→2→3 .
- Then, the second piece moves by the route 1→2 .
- Then, the first piece moves by the route 3→4→3→2→1 .
- Then, the second piece moves by the route 2→1 .