#P85. [CSP-S 2022] Network test

[CSP-S 2022] Network test

Person in Charge

Attention

The memory limit for this problem is 1024 MB.

Problem Description

Little C is designing a routing system in a computer network.

The test network consists of nn hosts numbered from 11 to nn in sequence. These nn hosts are connected by n1n-1 network cables, where the ii-th cable connects host aia_i and host bib_i. It is guaranteed that any two hosts can be connected directly or indirectly through a finite number of network cables. Restricted by the transmission power of information, a host aa can transmit information directly to a host bb if and only if the two hosts are connected by no more than kk network cables directly or indirectly.

In computer networks, data transmission often requires several forwarding steps. Suppose Little C needs to transmit data from host aa to host bb (aba \neq b). He will select a sequence of hosts for transmission c1=a,c2,,cm1,cm=bc_1 = a, c_2, \ldots, c_{m-1}, c_m = b, and forward the data according to the following rule: for all 1i<m1 \le i < m, host cic_i sends the information directly to host ci+1c_{i+1}.

Each host requires a certain amount of time to process information, and the ii-th host takes viv_i units of time for processing. Data transmission in the network is extremely fast, so the transmission time is negligible. Thus, the time taken for the above transmission process is i=1mvci\sum_{i = 1}^{m} v_{c_i}.

There are a total of qq data transmission requests. The ii-th request is to send data from host sis_i to host tit_i. Little C wants to know the minimum number of time units required to complete the transmission for each request.

Input Format

The first line of input contains three positive integers n,Q,kn, Q, k, representing the number of hosts in the network, the number of requests, and the transmission parameter respectively. The data guarantees 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, where the ii-th integer denotes viv_i, with the constraint 1vi1091 \le v_i \le 10^9.

The next n1n-1 lines each contain two positive integers ai,bia_i, b_i, representing a network cable connecting host aia_i and host bib_i. It is guaranteed that 1ai,bin1 \le a_i, b_i \le n.

The next QQ lines each contain two positive integers si,tis_i, t_i, representing 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 \neq t_i.

Output Format

Output QQ lines, each with a positive integer representing the minimum time required for the ii-th transmission request.

Samples

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

Sample (1) Explanation

For the first request, since at least 4 network cables are required to connect host 44 and host 77, data cannot be transmitted directly between the two hosts and requires at least one forwarding step. We choose to forward the data at host 11. It is easy to find that host 11 is connected to both host 44 and host 77 with only two network cables, and the data processing time of host 11 is only 11, which is the minimum among all hosts. Therefore, the minimum transmission time is 4+1+7=124 + 1 + 7 = 12.

For the third request, since host 11 and host 22 are connected by only one network cable, direct transmission of data is the optimal solution, with a minimum transmission time of 1+2=31 + 2 = 3.

Sample (2)

See transmit/transmit2.in and transmit/transmit2.ans in the contestant's directory.

This sample satisfies the constraints of Test Case 2.

Sample (3)

See transmit/transmit3.in and transmit/transmit3.ans in the contestant's directory.

This sample satisfies the constraints of Test Case 3.

Sample (4)

See transmit/transmit4.in and transmit/transmit4.ans in the contestant's directory.

This sample satisfies the constraints of Test Case 20.

Data Range

For all test data, 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 \neq t_i.

Test Case No. 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 10510^5
171917 \sim 19 2×1052 \times 10^5 No
2020 5×1045 \times 10^4 33 Yes
212221 \sim 22 10510^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 randomly selected from 1,2,,i1, 2, \ldots, i with equal probability.