luogu#P5024. [NOIP 2018 提高组] 保卫王国

    ID: 4039 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2018倍增NOIP 提高组树链剖分动态 DP全局平衡二叉树

[NOIP 2018 提高组] 保卫王国

Background

NOIP 2018 Senior D2T3.

Problem Description

Country Z has nn cities and (n1)(n - 1) 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 ii is pip_i.

Little Z quickly finds a deployment plan with the minimum total cost. However, the king then提出 (tíchū) mm 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 jj-th requirement can be satisfied together with the deployment conditions above (without considering any other requirements beyond the jj-th one), output the minimum total cost under this requirement. If the jj-th requirement cannot be satisfied, output 1-1. Please help Little Z.

Input Format

The first line contains two integers and a string, in order: the number of cities nn, the number of requirements mm, and the data type typetype. typetype 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 nn integers. The ii-th integer denotes the cost pip_i of stationing an army in city ii.

The next (n1)(n - 1) lines each contain two integers u,vu, v, indicating a two-way road between uu and vv.

The next mm lines each contain four integers a,x,b,ya, x, b, y, describing one requirement: station xx armies in city aa, and station yy armies in city bb. Here, x,yx, y can only be 00 or 11:

  • If xx is 00, city aa must not have an army stationed.
  • If xx is 11, city aa must have an army stationed.
  • If yy is 00, city bb must not have an army stationed.
  • If yy is 11, city bb 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 mm lines. Each line contains one integer. The jj-th line represents the minimum total cost when satisfying the king’s jj-th requirement. If the king’s jj-th requirement cannot be satisfied, output 1-1 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 44 and 55.
  • For the second requirement, the minimum cost is achieved by stationing armies in cities 11, 22, and 33.
  • The third requirement cannot be satisfied, because not stationing armies in both cities 11 and 55 means there exists a road whose two directly connected cities both have no armies stationed.

Constraints

Test Point ID type\text{type} n=m=n = m=
1,21,2 A3 1010
3,43,4 C3
5,65,6 A3 100100
77 C3
8,98,9 A3 2×1032\times 10^3
10,1110,11 C3
12,1312,13 A1 10510^5
14,15,1614, 15, 16 A2
1717 A3
18,1918,19 B1
20,2120,21 C1
2222 C2
23,24,2523, 24, 25 C3

Meaning of the data types:

  • A: City ii is directly connected to city i+1i + 1.
  • B: The distance from any city to city 11 is at most 100100 (distance is defined as the number of edges on the shortest path). That is, if the tree is rooted at city 11, its depth is at most 100100.
  • C: There are no special constraints on the shape of the tree.
  • 1: In each query, it is guaranteed that a=1,x=1a = 1, x = 1, meaning city 11 must have an army stationed. There are no restrictions on b,yb, y.
  • 2: In each query, it is guaranteed that a,ba, b are adjacent (directly connected by one road).
  • 3: There are no special constraints on the queries.

For 100%100\% of the testdata, it is guaranteed that 1n,m1051 \leq n, m \leq 10^5, 1pi1051 \leq p_i \leq 10^5, 1u,v,a,bn1 \leq u, v, a, b \leq n, aba \neq b, and x,y{0,1}x, y \in \{0, 1\}.

Translated by ChatGPT 5