QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 256 MB Total points: 100

#5265. 谷物

Statistics

Bajtocja 是一个美丽的国度,由 Bajtazar $22^2$ 世国王公正地统治。该国共有 $n$ 个村庄(编号从 $1$ 到 $n$),由一个包含 $n-1$ 条道路的连通网络连接。在 $1$ 号村庄处矗立着国王的城堡。

国王有 $k$ 个儿子,他们很快就要成年了。成年后的王子将需要拥有自己的城堡,因此在某些村庄会建立新的城堡。

国王和他的儿子们需要就 Bajtocja 的安全等事务进行沟通。为此,每天都会有信使从每个城堡出发,向其他所有城堡发送消息。信使的坐骑在每次出发前必须摄入适量的谷物。具体来说,每经过 $1$ 公里的路程,坐骑就需要吃掉 $1$ 十克(dekagram)的谷物。

请编写一个程序,计算在每新建一座城堡后,Bajtocja 每天所需的谷物总量。请注意,为了使两座城堡之间能够完全通信,它们会派出两名信使:一名从第一座城堡出发将消息送到第二座城堡,另一名则反之。

输入格式

输入的第一行包含两个整数 $n$ 和 $k$ ($1 \le k < n \le 100\,000$),分别表示 Bajtocja 的村庄数量和王子数量。

接下来的 $n-1$ 行描述了 Bajtocja 的道路网络:每行包含三个整数 $a, b$ 和 $c$ ($1 \le a, b \le n, 1 \le c \le 1000$),表示连接编号为 $a$ 和 $b$ 的村庄的道路,其长度为 $c$ 公里。

接下来的 $k$ 行描述了王子们建立城堡的村庄编号。每行包含一个整数 $d$ ($2 \le d \le n$)。每个村庄最多只能建立一座城堡(即后续的 $d$ 值互不相同)。

输出格式

你的程序应输出 $k$ 行;第 $i$ 行应包含一个整数,表示在第 $i$ 位王子建立城堡后,信使坐骑所需的谷物总量(以十克为单位)。

样例

输入格式 1

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

输出格式 1

8
40
90

测试用例

  1. $n = 1001, k = 1000$;除 $1$ 号村庄外,每个村庄都与 $1$ 号村庄相连;道路长度均为 $1$。
  2. $n = 100\,000, k = n - 1$;村庄 $1$ 到 $n$ 位于同一条路径上;王子们按顺序占据这些村庄;道路长度均为 $1000$。

评分

测试集分为以下子任务。每个子任务的测试用例由一组或多组独立的测试数据组成。

子任务 条件 分值
1 $n \cdot k \le 100\,000$ 15
2 村庄位于同一条路径上,编号从 $1$ 到 $n$ 依次递增 35
3 无附加条件 50

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.