QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 32 MB 満点: 100

#13522. 旅行商拜特萨尔

統計

旅行商 Byteasar 在 Byteotia 辛勤地奔波。在过去,旅行商可以自己选择想要访问的城市以及访问的顺序,但那样的日子已经一去不复返了。自从“旅行商中央控制办公室”成立以来,每位旅行商都会从办公室收到一份要访问的城市列表以及他们的旅行顺序。正如中央办公室通常会发生的那样,被强加的城市访问顺序与最优顺序相去甚远。在出发旅行之前,Byteasar 至少想知道他访问完所有城市需要花费多少时间。你的任务就是计算这个时间。

Byteotia 的城市从 $1$ 到 $n$ 进行编号。Byteotia 的首都编号为 $1$,这也是 Byteasar 开始旅程的地方。这些城市由双向道路网络连接。在由道路直接连接的两个城市之间旅行总是需要花费 $1$ 个单位的时间。从首都出发,可以到达 Byteotia 的任何其他城市。然而,道路网络的设计非常节约,因此道路绝不会形成环。

任务

编写一个程序:

  • 从标准输入读取 Byteotia 道路网络的描述以及 Byteasar 必须访问的城市列表,
  • 计算 Byteasar 旅行的总时间,
  • 将结果写入标准输出。

输入格式

标准输入的第一行包含一个整数 $n$,表示 Byteotia 的城市数量,$1 \le n \le 30\,000$。

接下来的 $n-1$ 行描述了道路网络。在这些行中,每行包含两个整数 $a$ 和 $b$($1 \le a,b \le n$ 且 $a\ne b$),表示城市 $a$ 和 $b$ 之间有一条道路连接。

第 $n+1$ 行包含一个整数 $m$,表示 Byteasar 需要访问的城市数量,$1 \le m \le 5\,000$。

接下来的 $m$ 行包含 Byteasar 路线中依次访问的城市编号——每行一个编号。

输出格式

标准输出的第一行也是唯一一行应该包含一个整数,表示 Byteasar 旅行的总时间。

样例

输入样例 1

5
1 2
1 5
3 5
4 5
4
1
3
2
5

输出样例 1

7

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.