luogu#P5024. [NOIP 2018 提高组] 保卫王国
[NOIP 2018 提高组] 保卫王国
Background
NOIP 2018 Senior D2T3.
Problem Description
Country Z has cities and two-way roads. Each road connects two cities, and any two cities can reach each other through some roads.
The Minister of Defense, Little Z, wants to station armies in the cities. The deployment must satisfy the following conditions:
- A city may have an army stationed, or may have no army stationed.
- For any two cities directly connected by a road, at least one of them must have an army stationed.
- Stationing an army in a city costs money. The cost of stationing an army in city is .
Little Z quickly finds a deployment plan with the minimum total cost. However, the king then提出 (tíchū) requirements. Each requirement specifies whether two particular cities must have armies stationed or must not have armies stationed. Little Z needs to answer each requirement one by one.
Specifically, if the king’s -th requirement can be satisfied together with the deployment conditions above (without considering any other requirements beyond the -th one), output the minimum total cost under this requirement. If the -th requirement cannot be satisfied, output . Please help Little Z.
Input Format
The first line contains two integers and a string, in order: the number of cities , the number of requirements , and the data type . is a string consisting of an uppercase letter A, B, or C, followed by a digit 1, 2, or 3. It can help you get partial scores. You may not need to use this parameter. Its meaning is described in detail in the “Data Scale and Conventions” section.
The second line contains integers. The -th integer denotes the cost of stationing an army in city .
The next lines each contain two integers , indicating a two-way road between and .
The next lines each contain four integers , describing one requirement: station armies in city , and station armies in city . Here, can only be or :
- If is , city must not have an army stationed.
- If is , city must have an army stationed.
- If is , city must not have an army stationed.
- If is , city must have an army stationed.
In the input file, any two adjacent data items on the same line are separated by one space.
Output Format
Output lines. Each line contains one integer. The -th line represents the minimum total cost when satisfying the king’s -th requirement. If the king’s -th requirement cannot be satisfied, output on that line.
5 3 C3
2 4 1 3 9
1 5
5 2
5 3
3 4
1 0 3 0
2 1 3 1
1 0 5 0
12
7
-1
Hint
Sample 1 Explanation
- For the first requirement, the minimum cost is achieved by stationing armies in cities and .
- For the second requirement, the minimum cost is achieved by stationing armies in cities , , and .
- The third requirement cannot be satisfied, because not stationing armies in both cities and means there exists a road whose two directly connected cities both have no armies stationed.
Constraints
| Test Point ID | ||
|---|---|---|
A3 |
||
C3 |
||
A3 |
||
C3 |
||
A3 |
||
C3 |
||
A1 |
||
A2 |
||
A3 |
||
B1 |
||
C1 |
||
C2 |
||
C3 |
Meaning of the data types:
A: City is directly connected to city .B: The distance from any city to city is at most (distance is defined as the number of edges on the shortest path). That is, if the tree is rooted at city , its depth is at most .C: There are no special constraints on the shape of the tree.1: In each query, it is guaranteed that , meaning city must have an army stationed. There are no restrictions on .2: In each query, it is guaranteed that are adjacent (directly connected by one road).3: There are no special constraints on the queries.
For of the testdata, it is guaranteed that , , , , and .
Translated by ChatGPT 5