Maximize Spanning Tree Stability with Upgrades - Problem

You are given an integer n, representing n nodes numbered from 0 to n - 1 and a list of edges, where edges[i] = [u_i, v_i, s_i, must_i]:

  • u_i and v_i indicates an undirected edge between nodes u_i and v_i
  • s_i is the strength of the edge
  • must_i is an integer (0 or 1). If must_i == 1, the edge must be included in the spanning tree. These edges cannot be upgraded.

You are also given an integer k, the maximum number of upgrades you can perform. Each upgrade doubles the strength of an edge, and each eligible edge (with must_i == 0) can be upgraded at most once.

The stability of a spanning tree is defined as the minimum strength score among all edges included in it.

Return the maximum possible stability of any valid spanning tree. If it is impossible to connect all nodes, return -1.

Note: A spanning tree of a graph with n nodes is a subset of the edges that connects all nodes together (i.e. the graph is connected) without forming any cycles, and uses exactly n - 1 edges.

Input & Output

Example 1 — Basic Case
$ Input: n = 4, edges = [[0,1,4,1],[1,2,2,0],[2,3,3,0],[0,3,5,0]], k = 2
Output: 6
💡 Note: We can upgrade edges [1,2] (2→4) and [2,3] (3→6). With mandatory edge [0,1] strength 4, we build spanning tree with edges (0,1,4), (0,3,5), (2,3,6). Minimum strength is 4, but if we use (0,1,4), (1,2,4), (2,3,6), minimum is 4. Actually, the optimal is to use edges that give stability 6.
Example 2 — No Upgrades Needed
$ Input: n = 3, edges = [[0,1,10,0],[1,2,10,0],[0,2,1,1]], k = 1
Output: 1
💡 Note: Mandatory edge [0,2] has strength 1, so maximum stability is limited to 1 regardless of upgrades.
Example 3 — Impossible Case
$ Input: n = 3, edges = [[0,1,5,0]], k = 1
Output: -1
💡 Note: Only 1 edge but need 2 edges for spanning tree of 3 nodes. Impossible to connect all nodes.

Constraints

  • 2 ≤ n ≤ 105
  • 1 ≤ edges.length ≤ 3 × 105
  • edges[i].length == 4
  • 0 ≤ ui, vi ≤ n - 1
  • 1 ≤ si ≤ 106
  • musti ∈ {0, 1}
  • 0 ≤ k ≤ edges.length

Visualization

Tap to expand
INPUTALGORITHMRESULT01234 (must=1)235n=4, k=2 upgradesRed edge is mandatoryGreen edges can be upgraded1Binary Search Range: [0, 10]2Check if stability=5 achievable3Filter edges ≥ 5 or upgradeable4Build spanning tree greedilyUse Union-Find to checkif valid spanning tree existsMaximum Stability5Achievable with 2 upgradesSpanning Tree Edges:(0,1): strength 4 (mandatory)(2,3): strength 6 (upgraded)(1,2): strength 4 (upgraded)Key Insight:Binary search on the stability value allows us to efficiently find the maximum achievableminimum edge strength in any valid spanning tree using at most k upgrades.TutorialsPoint - Maximize Spanning Tree Stability with Upgrades | Binary Search on Answer
Asked in
Google 25 Microsoft 18
8.9K Views
Medium Frequency
~45 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen