Maximize Sum of Weights after Edge Removals
Real-world version: Capping Links at Every Fibre HubThe statement here is our own: the same inputs, outputs and technique.
A fibre network is described by links. There are links.length + 1 hubs, numbered from 0, and each entry links[i] = [a, b, w] is a two-way fibre run between hub a and hub b carrying w gigabits. The runs form a tree: every hub reaches every other hub along exactly one chain of runs.
The switches being installed are small, so a hub can terminate at most ports of the runs that touch it. Any run not terminated at both of its hubs has to be pulled out.
Decide which runs to pull out so that every hub ends up touched by at most ports of the remaining runs, and return the largest total gigabits the remaining runs can carry.
Examples
Example 1
- Input
- links = [[0, 1, 5], [0, 2, 3], [0, 3, 4]], ports = 1
- Output
- 5
All three runs touch hub 0, which may terminate only one of them. Keeping the run to hub 1 leaves every hub touched by at most one run, for a total of 5 gigabits.
Example 2
- Input
- links = [[0, 1, 3], [1, 2, 5], [2, 3, 3]], ports = 1
- Output
- 6
Keeping the runs [0, 1] and [2, 3] touches each of the four hubs exactly once, which is within the single port every hub has, and carries 6 gigabits.
Example 3
- Input
- links = [[0, 1, 4], [1, 2, 6], [2, 3, 4]], ports = 3
- Output
- 14
No hub here touches more than two runs, so with three ports each nothing has to be pulled out and the total is 14 gigabits.
Constraints
- n == links.length + 1
- 2 <= n <= 10^5
- 1 <= ports <= n - 1
- links[i].length == 3
- 0 <= links[i][0], links[i][1] <= n - 1
- 1 <= links[i][2] <= 10^6
- The runs form a tree over the n hubs.
- The answer is at most (n - 1) * 10^6, so it stays below 10^11.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def max_kept_bandwidth(links: list[list[int]], ports: int) -> int:public long maxKeptBandwidth(int[][] links, int ports)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- House Robber IIIReal-world version: Bonus Pool Without Direct ReportsMedium
- Binary Tree Vertical Order TraversalReal-world version: Wall Panel ColumnsMedium
- Binary Tree Maximum Path SumReal-world version: Best Chamber RunHard
- Vertical Order Traversal of a Binary TreeReal-world version: Notch Readout of a Hanging MobileHard