CF1774E.Two Chess Pieces

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Cirno_9baka has a tree with nn 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 11 of the tree. In one step, you can choose any piece, and move it to the neighboring node. You are also given an integer dd. You need to ensure that the distance between the two pieces doesn't ever exceed dd.

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.

Cirno_9baka 有一棵包含 nn 个节点的树,他愿意与你共享这棵树,也就是说你可以对它进行操作。

初始时,树的节点 11 上放置着两枚棋子。在每一步中,你可以任选其中一枚棋子,并将其移动到一个相邻的节点。同时,你被给定一个整数 dd,要求在整个过程中,两枚棋子之间的距离始终不超过 dd。

这两枚棋子各自拥有一条必须经过的节点序列(顺序不限),且最终都必须返回根节点(即节点 11)。作为一个充满好奇心的男孩,他想知道完成所有要求所需的最少步数。

输入格式

The first line contains two integers nn and dd (2≤d≤n≤2⋅1052 \le d \le n \le 2\cdot 10^5).

The ii-th of the following n−1n - 1 lines contains two integers ui,viu_i, v_i (1≤ui,vi≤n)(1 \le u_i, v_i \le n), denoting the edge between the nodes ui,viu_i, v_i of the tree.

It's guaranteed that these edges form a tree.

The next line contains an integer m1m_1 (1≤m1≤n1 \le m_1 \le n) and m1m_1 integers a1,a2,…,am1a_1, a_2, \ldots, a_{m_1} (1≤ai≤n1 \le a_i \le n, all aia_i are distinct) — the sequence of nodes that the first piece needs to pass.

The second line contains an integer m2m_2 (1≤m2≤n1 \le m_2 \le n) and m2m_2 integers b1,b2,…,bm2b_1, b_2, \ldots, b_{m_2} (1≤bi≤n1 \le b_i \le n, all bib_i are distinct) — the sequence of nodes that the second piece needs to pass.

第一行包含两个整数 nn 和 dd(2≤d≤n≤2⋅1052 \le d \le n \le 2\cdot 10^5)。

接下来的 n−1n - 1 行中,第 ii 行包含两个整数 ui,viu_i, v_i(1≤ui,vi≤n1 \le u_i, v_i \le n),表示树中节点 uiu_i 与 viv_i 之间的一条边。

保证这些边构成一棵树。

下一行包含一个整数 m1m_1(1≤m1≤n1 \le m_1 \le n)以及 m1m_1 个整数 a1,a2,…,am1a_1, a_2, \ldots, a_{m_1}(1≤ai≤n1 \le a_i \le n,所有 aia_i 互不相同)—— 表示第一个棋子需要依次经过的节点序列。

再下一行包含一个整数 m2m_2(1≤m2≤n1 \le m_2 \le n)以及 m2m_2 个整数 b1,b2,…,bm2b_1, b_2, \ldots, b_{m_2}(1≤bi≤n1 \le b_i \le n,所有 bib_i 互不相同)—— 表示第二个棋子需要依次经过的节点序列。

输出格式

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 66.

  • The second piece moves by the route 1→2→4→2→11 \to 2 \to 4 \to 2 \to 1.

  • Then, the first piece moves by the route 1→3→11 \to 3 \to 1.

In the second sample, here is one possible sequence of steps of length 88:

  • The first piece moves by the route 1→2→31 \to 2 \to 3.

  • Then, the second piece moves by the route 1→21 \to 2.

  • Then, the first piece moves by the route 3→4→3→2→13 \to 4 \to 3 \to 2 \to 1.

  • Then, the second piece moves by the route 2→12 \to 1.

在第一个样例中,以下是一种长度为 66 的可能操作序列:

  • 第二个棋子沿路径 1→2→4→2→11 \to 2 \to 4 \to 2 \to 1 移动。

  • 然后,第一个棋子沿路径 1→3→11 \to 3 \to 1 移动。

在第二个样例中,以下是一种长度为 88 的可能操作序列:

  • 第一个棋子沿路径 1→2→31 \to 2 \to 3 移动。

  • 然后,第二个棋子沿路径 1→21 \to 2 移动。

  • 然后,第一个棋子沿路径 3→4→3→2→13 \to 4 \to 3 \to 2 \to 1 移动。

  • 然后,第二个棋子沿路径 2→12 \to 1 移动。

输入解题思路,AI测评打分。不知道怎么写?

首页