#P8820. [CSP-S 2022] 数据传输

    ID: 7587 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2022O2优化树链剖分矩阵乘法CSP S 提高级

[CSP-S 2022] 数据传输

Description

Xiao C is designing a routing system for a computer network.

The test network has nn hosts, numbered 1n1 \sim n. These nn hosts are connected by n1n - 1 network cables; the ii-th cable connects hosts aia_i and bib_i. It is guaranteed that any two hosts are connected directly or indirectly through a finite number of cables. Due to transmission power limits, host aa can directly send information to host bb if and only if the two hosts are connected directly or indirectly by a path that uses at most kk cables.

In a computer network, data transmission often requires several forwards. Suppose Xiao C needs to send data from host aa to host bb (aba \ne b). He will choose several hosts for transmission c1=a,c2,,cm1,cm=bc_1 = a, c_2, \ldots, c_{m - 1}, c_m = b, and forward as follows: for all 1i<m1 \le i < m, host cic_i directly sends information to ci+1c_{i + 1}.

Each host needs some time to process information. The ii-th host needs viv_i units of time. Data transmission in the network is very fast, so the transmission time can be ignored. Therefore, the total time cost of the above process is i=1mvci\sum_{i = 1}^{m} v_{c_i}.

There are QQ data sending requests in total. In the ii-th request, data is sent from host sis_i to host tit_i. Xiao C wants to know, for each request, the minimum number of time units needed to complete the transmission.

Input Format

The first line contains three positive integers n,Q,kn, Q, k, denoting the number of hosts, the number of requests, and the transmission parameter, respectively. It is guaranteed that 1n2×1051 \le n \le 2 \times {10}^5, 1Q2×1051 \le Q \le 2 \times {10}^5, 1k31 \le k \le 3.

The second line contains nn positive integers; the ii-th integer is viv_i, and it is guaranteed that 1vi1091 \le v_i \le {10}^9.

The next n1n - 1 lines; the ii-th line contains two positive integers ai,bia_i, b_i, indicating a network cable connecting hosts aia_i and bib_i. It is guaranteed that 1ai,bin1 \le a_i, b_i \le n.

The next QQ lines; the ii-th line contains two positive integers si,tis_i, t_i, indicating a request to send data from host sis_i to host tit_i. It is guaranteed that 1si,tin1 \le s_i, t_i \le n and sitis_i \ne t_i.

Output Format

Output QQ lines. Each line contains one positive integer, the minimum number of time units needed to complete the ii-th request.

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

12
12
3

Hint

【Sample Explanation #1】

For the first request, since hosts 44 and 77 require at least 44 cables to connect, data cannot be transmitted directly between the two hosts, so at least one forward is required. Let it be forwarded once at host 11. It is easy to see that both host 44 and host 77 are within two cables from host 11, and host 11 has a processing time of only 11, the minimum among all hosts. Therefore, the minimum transmission time is 4+1+7=124 + 1 + 7 = 12.

For the third request, since hosts 11 and 22 are connected by only 11 cable, direct transmission is optimal, and the minimum transmission time is 1+2=31 + 2 = 3.

【Sample #2】

See the attachments transmit/transmit2.in and transmit/transmit2.ans.

This sample satisfies the limits of test point 22.

【Sample #3】

See the attachments transmit/transmit3.in and transmit/transmit3.ans.

This sample satisfies the limits of test point 33.

【Sample #4】

See the attachments transmit/transmit4.in and transmit/transmit4.ans.

This sample satisfies the limits of test point 2020.

Constraints

For all testdata, it is guaranteed that 1n2×1051 \le n \le 2 \times {10}^5, 1Q2×1051 \le Q \le 2 \times {10}^5, 1k31 \le k \le 3, 1ai,bin1 \le a_i, b_i \le n, 1si,tin1 \le s_i, t_i \le n, and sitis_i \ne t_i.

Test point nn \le QQ \le k=k = Special property
11 1010 22 Yes
22 33
33 200200 22
454 \sim 5 33
676 \sim 7 20002000 11 No
898 \sim 9 22
101110 \sim 11 33
121312 \sim 13 2×1052 \times {10}^5 11
1414 5×1045 \times {10}^4 22 Yes
151615 \sim 16 105{10}^5
171917 \sim 19 2×1052 \times {10}^5 No
2020 5×1045 \times {10}^4 33 Yes
212221 \sim 22 105{10}^5
232523 \sim 25 2×1052 \times {10}^5 No

Special property: it is guaranteed that ai=i+1a_i = i + 1, and bib_i is chosen uniformly at random from 1,2,,i1, 2, \ldots, i.

Translated by ChatGPT 5