#P6594. [YsOI2020] 换寝室

    ID: 7334 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>二分O2优化深度优先搜索 DFS树形 DP最近公共祖先 LCA差分

[YsOI2020] 换寝室

Background

School is about to start, and Ysuperman is extremely busy assigning dorm rooms to the kids......

Problem Description

There are nn rooms in the kindergarten. These rooms are connected by n1n-1 bidirectional roads. The ii-th road connects rooms uiu_i and viv_i. Ysuperman can choose to open or close each road. With all roads open, every room can reach any other room.

Each room has a “difference value”. The difference value of room ii is hih_i.

After deciding which roads to close, the whole dormitory will be split into several connected components. For a connected component, the kids’ dissatisfaction value is defined as the maximum difference value minus the minimum difference value within that component. The kids’ total dissatisfaction value is defined as the maximum dissatisfaction value among all connected components.

There are mm dorm teachers. Each teacher checks rooms at night. The ii-th teacher will walk from room xix_i to room yiy_i. If a teacher passes through a closed road during the check, they will be very angry. A teacher’s dissatisfaction value is defined as the number of closed roads on the path from xix_i to yiy_i. The teachers’ total dissatisfaction value is defined as the sum of dissatisfaction values of all teachers.

The maximum teachers’ total dissatisfaction value that Ysuperman can tolerate is kk. Now Ysuperman wants to know the minimum possible value of the kids’ total dissatisfaction value.

Input Format

The input contains n+m+1n+m+1 lines.

The first line contains three integers n,m,kn, m, k, representing the number of rooms, the number of dorm teachers, and the maximum teachers’ total dissatisfaction value that Ysuperman can tolerate.

The next line contains nn integers. The ii-th integer is hih_i, representing the difference value of room ii.

The next n1n-1 lines each contain two integers. Line i+2i+2 gives uiu_i and viv_i, indicating that there is a direct road between dorms uiu_i and viv_i.

The next mm lines each contain two integers. Line i+n+1i+n+1 gives xix_i and yiy_i, representing the ii-th teacher’s checking route.

Output Format

Output one integer in one line, which is the answer.

5 2 0
1 3 1 4 0
1 2
1 3
1 4
1 5
2 3
1 4
3
5 2 1
1 3 1 4 0
1 2
1 3
1 4
1 5
2 3
1 4
2

Hint

Sample Explanation

Sample Explanation 11

Ysuperman chooses to close the road connecting 11 and 55. The teachers’ total dissatisfaction value is 00. The dormitory is split into 22 connected components, and the kids’ total dissatisfaction value is 33.

Sample Explanation 22

The graph is the same as in Sample 1.

Ysuperman chooses to close the road connecting 11 and 55 and also the road connecting 11 and 44. The teachers’ total dissatisfaction value is 11. The dormitory is split into 33 connected components, and the kids’ total dissatisfaction value is 22.


Constraints

This problem uses bundled testdata.

Subtask nn mm kk Special Property Score
1 20\le 20 10\le 10 80\le 80 None 8
2 150\le 150 103\le 10^3 8×104\le 8 \times 10^4 13
3 800\le 800 105\le 10^5 8×107\le 8 \times 10^7 The tree is a chain
4 The tree is a fully blooming chrysanthemum
5 =0= 0 None
6 8×107\le 8 \times 10^7 40

[A chain] is defined as: all nodes have degree 2\le 2.

[A fully blooming chrysanthemum] is defined as: there exists a node with degree n1n-1.

For 100%100\% of the data, it holds that 1hi1091 \le h_i \le 10^9, 0k81070 \le k \le 8 \cdot 10^7, and uiviu_i \ne v_i.

Translated by ChatGPT 5