#P9424. [蓝桥杯 2023 国 B] 删边问题
[蓝桥杯 2023 国 B] 删边问题
Problem Description
Given an undirected graph with nodes and edges, where the nodes are numbered . Each node has a weight .
You may choose exactly one edge from the edges and delete it. If the remaining graph contains exactly 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 and , respectively. Please find a plan that makes the difference between and as small as possible, and output the difference between and .
Input Format
The first line contains two integers and .
The second line contains integers .
The following lines each contain two integers and , indicating that there is an edge between node and node .
Output Format
Output one integer representing the minimum difference. If there is no valid deletion plan, output .
4 4
10 20 30 40
1 2
2 1
2 3
4 3
20
Hint
Sample Explanation
Since there are actually edges between and , there are valid deletion plans: deleting the edge between and deleting the edge between .
If we delete the edge between , the remaining graph contains connected components: and . Their weight sums are and , and the difference is .
If we delete the edge between , the remaining graph contains connected components: and . Their weight sums are and , and the difference is .
Constraints and Notes for Test Cases
- For of the testdata, .
- For another of the testdata, the degree of each node is at most .
- For of the testdata, , , .
The 14th Lanqiao Cup Software Contest Finals, C/C++ College Group B, Problem F.
Translated by ChatGPT 5