Choose a spanning tree and optionally upgrade edge weights to maximize the tree's total stability value.
Problem
You are given an undirected connected graph with n nodes and weighted edges. You may select a spanning tree of the graph. Some edges can be upgraded before or during selection according to the problem rules, which increases their effective weight.
Your goal is to build a spanning tree with the maximum possible total stability after applying upgrades optimally.
Return the best achievable stability value.
Notes
- A spanning tree connects all vertices using exactly
n - 1edges and contains no cycles. - The upgrade operation changes the value of an edge in a beneficial way, so the optimal solution must balance which edges to keep and which edges to upgrade.
- Because the graph is connected, a valid spanning tree always exists.
Input/Output Style
This is an algorithmic interview problem. The exact input format may vary by platform wrapper; the core task is to compute the optimal spanning tree value under upgrade constraints.
Input Format
- An integer
nfor the number of vertices. - A list of undirected weighted edges.
- Optional upgrade information describing which edges or edge values can be improved.
- The graph is connected.
Output Format
- Return one integer: the maximum achievable total stability of a spanning tree.
Constraints
1 <= n- The graph is connected.
- Edge weights and upgrade effects fit in 32-bit signed integers unless otherwise specified by the wrapper.
- A spanning tree must contain exactly
n - 1edges.
Example 1
Input
n = 4 edges = [[1,2,4],[2,3,1],[3,4,3],[1,4,2],[1,3,5]] upgrades = [[2,3,4]]
Output
12
Explanation
If edge (2,3) can be upgraded from 1 to 4, the best spanning tree can use edges (1,3)=5, (1,2)=4, and (2,3)=4 for a total of 12.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.