#P9432. [NAPC-#1] rStage5 - Hard Conveyors

    ID: 10462 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>O2优化最近公共祖先 LCA树链剖分

[NAPC-#1] rStage5 - Hard Conveyors

Background

Two new groups of hack testdata have been added to this problem.


Harder, so it may be more fragile, so it is easier to break (confirmed).

You only spent two hours to instantly clear the main city s1~s10, and now you are here to instantly clear the reverse city.

Problem Description

The difference between this problem and Stage5 is that the definition of a valid path is different (the bold part in the brief statement is different). (Actually there are also: different samples, different data, different partial scores.)

Brief Statement

You are given an unrooted tree with nn nodes and kk key nodes on the tree. Each edge has a weight (i.e. the edge length). There are qq queries. Each query gives s,ts, t, and asks for the shortest length of a path from ss to tt that passes through at least one key node.

Input Format

The first line contains three positive integers n,q,kn, q, k, representing the number of nodes in the tree, the number of queries, and the number of key nodes.

The next n1n - 1 lines each contain three positive integers u,v,wu, v, w, indicating that there is an edge (u,v)(u, v) in the tree with weight ww. It is guaranteed that the input forms a tree.

The next line contains kk pairwise distinct positive integers, representing the indices of the key nodes.

The next qq lines each contain two positive integers s,ts, t, representing a query.

Output Format

For each query, output one line with one non-negative integer, representing the shortest length of a valid path for this query.

Note that a valid path may traverse no edges; in this case, the path length is 00.

7 6 2
1 2 3
1 3 5
3 4 2
3 5 4
2 6 1
1 7 1
2 3
2 3
2 1
7 1
4 5
6 6
2 2
8
3
7
6
2
0

Hint

Constraints

upd at 2023-6-25: Two new groups of hack testdata were added and placed in Sub6\text{Sub}\textsf6. They do not count toward the score.

This problem uses bundled tests.

$$\def\r{\cr\hline} \def\None{\text{None}} \def\arraystretch{1.5} \begin{array}{c|c|c|c} \textbf{Subtask} & \text{测试点编号} & \textbf{Sp. Constraints} & \textbf{Score}\r \textsf1&1\sim2 & k=n & 5 \r \textsf2&16\sim20 &k=1,n\leqslant10^3,q\leqslant10^3 & 15 \r \textsf3&21\sim25 & n\leqslant10^3,q\leqslant10^3 & 15 \r \textsf4&3\sim7 & q=1 & 15 \r \textsf5&8\sim15 & - & 50 \r \textsf6&26\sim27 & - & 0 \r \end{array}$$

For 100%100\% of the data, 1n1051\leqslant n\leqslant 10^5, 1q1051\leqslant q\leqslant 10^5, 1kn1\leqslant k\leqslant n, 1w1041\leqslant w\leqslant 10^4, 1u,v,s,tn1\leqslant u,v,s,t\leqslant n.

Sample Explanation #1

The bold nodes in the figure are key nodes.

For each query, the following is one optimal path (there may be multiple optimal paths):

  1. 2132\to1\to3.
  2. 212\to1.
  3. 71217\to1\to2\to1.
  4. 4354\to3\to5.
  5. 6266\to2\to6.
  6. 22 (a valid path may traverse no edges; in this case, the path length is 00).

Translated by ChatGPT 5