Maximize Sum of Weights after Edge Removals - Problem
Maximize Sum of Weights after Edge Removals

You are given an undirected tree with n nodes numbered from 0 to n - 1. The tree is represented by a 2D array edges where edges[i] = [u_i, v_i, w_i] indicates there is an edge between nodes u_i and v_i with weight w_i.

Your goal is to strategically remove some edges such that:
โ€ข Each remaining node has at most k connections
โ€ข The total weight of remaining edges is maximized

Think of it as pruning a weighted tree to satisfy degree constraints while keeping the most valuable connections. This is a classic tree dynamic programming problem that requires careful consideration of each node's optimal edge selection.

Return the maximum possible sum of weights for the remaining edges.

Input & Output

example_1.py โ€” Basic Tree
$ Input: edges = [[0,1,4],[1,2,2],[2,3,12],[3,4,6]], k = 2
โ€บ Output: 22
๐Ÿ’ก Note: We can keep edges (2,3) with weight 12, (3,4) with weight 6, and (0,1) with weight 4. Each node has at most 2 connections and total weight is 12+6+4=22.
example_2.py โ€” Star Graph
$ Input: edges = [[0,1,10],[0,2,20],[0,3,15],[0,4,5]], k = 2
โ€บ Output: 35
๐Ÿ’ก Note: Node 0 can keep at most 2 edges. We choose the heaviest ones: (0,2) with weight 20 and (0,3) with weight 15. Total weight is 20+15=35.
example_3.py โ€” Small Tree
$ Input: edges = [[0,1,1],[1,2,3]], k = 1
โ€บ Output: 3
๐Ÿ’ก Note: With k=1, we can keep at most one edge per node. The optimal choice is edge (1,2) with weight 3, giving us maximum weight of 3.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค k โ‰ค n-1
  • edges.length == n-1
  • 0 โ‰ค ui, vi < n
  • 1 โ‰ค wi โ‰ค 106
  • The input forms a valid tree (connected and acyclic)

Visualization

Tap to expand
Network Infrastructure Optimization (k=2)Server 0Server 1Server 2Server 3Server 4100 Mbps150 Mbps50 Mbps80 MbpsOptimization AnalysisServer 0 (Root):Available: 100, 150, 50 Mbpsโœ“ Select top 2: 150 + 100 = 250โœ— Reject: 50 MbpsServer 1:Available: 80 Mbps (to Server 4)โœ“ Select: 80 MbpsServer 2:No additional connectionsAlready connected via Server 0Total Capacity:250 + 80 = 330 MbpsConstraint satisfied: each server โ‰ค 2 connections๐ŸŽฏ Key Insight: Use DFS to coordinate decisions across the network topology
Understanding the Visualization
1
Model as Tree
Each server is a node, each network cable is a weighted edge
2
Apply Constraints
Each server can maintain at most k active connections due to bandwidth limits
3
Greedy Selection
For each server, choose the k highest-bandwidth connections to maximize total capacity
4
DFS Coordination
Use post-order traversal to ensure child decisions inform parent choices
Key Takeaway
๐ŸŽฏ Key Insight: Tree DP with post-order DFS allows each node to optimally select its k best edges while coordinating with the overall tree structure
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 31
23.4K Views
Medium-High Frequency
~25 min Avg. Time
892 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