Skip to main content
Back to problems
Leetcode
Medium
Graphs
Minimum Spanning Tree
Greedy
Union Find
Google
Maximize Spanning Tree Stability With Upgrades

Choose a spanning tree and optionally upgrade edge weights to maximize the tree's total stability value.

Acceptance 0%
Problem Statement

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 - 1 edges 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 n for 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 - 1 edges.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.