#P14456. [ICPC 2025 Xi'an R] January's Color

[ICPC 2025 Xi'an R] January's Color

题目描述

Given a rooted tree with nn vertices, where the root is at vertex 11. It is guaranteed that no vertex has exactly one child in the tree. In other words, each vertex is either a leaf or has at least two\textbf{at least two} children. You own some of the vertices in the tree.

You may have some vertices in your hand. You can obtain new vertices in the following two ways:

  • Directly purchase a vertex ii from the bank at a cost of cic_i.
  • Select two different\textbf{two different} vertices from your hand that share the same parent, discard them, and obtain their parent for free.

Now, you have mm queries. In each query, you initially have exactly one vertex xx, and you want to end up with exactly one vertex yy with no other vertices left. Find the minimum cost needed to achieve this, or report no solution.

输入格式

The input consists of multiple test cases. The first line contains an integer tt (1t1051 \leq t \leq 10^5), the number of test cases. For each test case:

  • The first line contains two integers nn and mm (3n3105,1m31053 \le n \le 3 \cdot 10^5, 1 \le m \le 3 \cdot 10^5), where nn is the number of vertices in the tree and mm is the number of queries.
  • The second line contains nn integers c1,c2,,cnc_1, c_2, \ldots, c_n (1ci1091 \le c_i \le 10^9), which are the costs to obtain each vertex from the bank.
  • The next n1n - 1 lines each contain two integers uu and vv (1u,vn,uv1 \le u, v \le n, u \neq v), representing an edge in the tree.
  • The next mm lines each contain two integers xx and yy (1x,yn1 \le x, y \le n, xyx \neq y), representing a query.

It is guaranteed that the sum of nn and the sum of mm over all test cases do not exceed 31053 \cdot 10^5.

输出格式

For each query, output a single integer representing the minimum additional cost for that query. If there is no way to end up with a single vertex yy, output 1-1.

3
5 5
1 2 3 4 5
1 2
1 3
2 4
2 5
3 1
2 1
4 1
5 1
5 2
5 5
1 5 1 1 1
1 2
1 3
2 4
2 5
3 1
2 1
4 1
5 1
2 5
6 5
9 9 8 2 4 4
1 2
1 3
1 4
1 5
1 6
2 1
3 1
4 1
5 1
6 1
2
3
8
7
4
2
1
2
2
-1
2
2
4
2
2

提示

For the first query in the first test case, you initially have vertex 33 and wish to obtain vertex 11. The cheapest solution is to directly purchase vertex 22 at a cost of c2=2c_2=2, then discard vertices 22 and 33 to obtain their common parent 11 for free. It can be proven that this is the minimum-cost solution.

For the first query in the second test case, you again start with vertex 33 and want to obtain vertex 11. Instead of purchasing vertex 22, you choose to purchase vertices 44 and 55 at a total cost of c4+c5=2c_4 + c_5 = 2, then discard vertices 44 and 55 to obtain their parent vertex 22 for free. Next, discard vertices 22 and 33 to obtain their parent vertex 11 for free. It can be proven that this is the minimum-cost solution.

Note that you can not purchase another vertex 33 and then discard it with the original vertex 33 to obtain vertex 11, since these two vertices 33 do not satisfy the requirement of being two different vertices.