#P9424. [蓝桥杯 2023 国 B] 删边问题

[蓝桥杯 2023 国 B] 删边问题

Problem Description

Given an undirected graph GG with NN nodes and MM edges, where the nodes are numbered 1N1 \cdots N. Each node has a weight WiW_i.

You may choose exactly one edge from the MM edges and delete it. If the remaining graph contains exactly 22 connected components, then this is called a valid deletion plan.

For a valid deletion plan, suppose the sums of node weights in the two connected components are XX and YY, respectively. Please find a plan that makes the difference between XX and YY as small as possible, and output the difference between XX and YY.

Input Format

The first line contains two integers NN and MM.

The second line contains NN integers W1,W2,,WNW_1, W_2, \cdots, W_N.

The following MM lines each contain two integers UU and VV, indicating that there is an edge between node UU and node VV.

Output Format

Output one integer representing the minimum difference. If there is no valid deletion plan, output 1-1.

4 4
10 20 30 40
1 2
2 1
2 3
4 3
20

Hint

Sample Explanation

Since there are actually 22 edges between 11 and 22, there are 22 valid deletion plans: deleting the edge between (2,3)(2, 3) and deleting the edge between (3,4)(3, 4).

If we delete the edge between (2,3)(2, 3), the remaining graph contains 22 connected components: {1,2}\{1,2\} and {3,4}\{3,4\}. Their weight sums are 3030 and 7070, and the difference is 4040.

If we delete the edge between (3,4)(3, 4), the remaining graph contains 22 connected components: {1,2,3}\{1,2,3\} and {4}\{4\}. Their weight sums are 6060 and 4040, and the difference is 2020.

Constraints and Notes for Test Cases

  • For 20%20\% of the testdata, 1N,M100001 \le N, M \le 10000.
  • For another 20%20\% of the testdata, the degree of each node is at most 22.
  • For 100%100\% of the testdata, 1N,M2000001 \le N, M \le 200000, 0Wi1090 \le W_i \le 10^9, 1U,VN1 \le U, V \le N.

The 14th Lanqiao Cup Software Contest Finals, C/C++ College Group B, Problem F.

Translated by ChatGPT 5