Second-best minimum spanning tree: find MST of second-minimum total weight.
Analyze the second-best minimum spanning tree: find mst of second-minimum total weight..
Examples
Input: "test_input_1"
Output: "output_1"
Input: "test_input_2"
Output: "output_2"
Hints
Start by recalling that the second-best MST must differ from the best MST by exactly one edge swap. Consider how adding an edge not in the best MST and removing an edge from the best MST can yield a new spanning tree.
After identifying the best MST, iterate over all edges not in the best MST and temporarily add them to the best MST. For each addition, determine which edge in the cycle formed must be removed to restore a spanning tree, then calculate the new total weight.
Optimize the previous approach by leveraging the fact that the second-best MST must share at least (n-2) edges with the best MST. Use this property to reduce the number of edge swaps you need to evaluate, focusing only on edges that can potentially yield a better second-best MST.
Second-best minimum spanning tree: find MST of second-minimum total weight.
Analyze the second-best minimum spanning tree: find mst of second-minimum total weight..