Skip to content

Practice Maximize Spanning Tree Stability with Upgrades

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3600
LeetCode Problem

Maximize Spanning Tree Stability with Upgrades

Problem Statement

Each undirected edge [u, v, strength, must] connects two of n nodes. All edges with must = 1 must belong to the chosen spanning tree and cannot be upgraded. At most k optional edges with must = 0 may each have their strength doubled once. A spanning tree connects all nodes with n - 1 edges and no cycle; its stability is its smallest edge strength. Return the greatest achievable stability, or -1 when no valid spanning tree exists.

Source: Maximize Spanning Tree Stability with Upgrades on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 3, edges = [[0,1,2,1],[1,2,3,0]], k = 1
Output
2

# Example 2

Input
n = 3, edges = [[0,1,4,0],[1,2,3,0],[0,2,1,0]], k = 2
Output
6

# Example 3

Input
n = 3, edges = [[0,1,1,1],[1,2,1,1],[2,0,1,1]], k = 0
Output
-1

Constraints

2 <= n <= 10^5
1 <= edges.length <= 10^5
edges[i] = [ui, vi, si, musti]
0 <= ui, vi < n
ui != vi
1 <= si <= 10^5
musti is either 0 or 1.
0 <= k <= n
There are no duplicate edges.

Related LeetCode Problems

Practice more problems similar to Maximize Spanning Tree Stability with Upgrades.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.