#P14456. [ICPC 2025 Xi'an R] January's Color
[ICPC 2025 Xi'an R] January's Color
题目描述
Given a rooted tree with vertices, where the root is at vertex . It is guaranteed that no vertex has exactly one child in the tree. In other words, each vertex is either a leaf or has 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 from the bank at a cost of .
- Select vertices from your hand that share the same parent, discard them, and obtain their parent for free.
Now, you have queries. In each query, you initially have exactly one vertex , and you want to end up with exactly one vertex 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 (), the number of test cases. For each test case:
- The first line contains two integers and (), where is the number of vertices in the tree and is the number of queries.
- The second line contains integers (), which are the costs to obtain each vertex from the bank.
- The next lines each contain two integers and (), representing an edge in the tree.
- The next lines each contain two integers and (, ), representing a query.
It is guaranteed that the sum of and the sum of over all test cases do not exceed .
输出格式
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 , output .
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 and wish to obtain vertex . The cheapest solution is to directly purchase vertex at a cost of , then discard vertices and to obtain their common parent 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 and want to obtain vertex . Instead of purchasing vertex , you choose to purchase vertices and at a total cost of , then discard vertices and to obtain their parent vertex for free. Next, discard vertices and to obtain their parent vertex for free. It can be proven that this is the minimum-cost solution.
Note that you can not purchase another vertex and then discard it with the original vertex to obtain vertex , since these two vertices do not satisfy the requirement of being two different vertices.
京公网安备 11011102002149号