Best Hub-and-Spokes Value Sum with a Spoke Limit
Trees & GraphsAuto-graded · 17 test casesLast updated
Choose a hub node and up to k adjacent nodes to maximize the total node value, which tests per-node greedy selection over adjacency lists.
You are given an undirected graph where every node carries an integer value, along with an integer k. Build a "hub-and-spokes" selection by picking one hub node and then some of its direct neighbors as spokes. Your job is to find the largest possible total value of such a selection.
Rules for a valid selection
- Exactly one node is the hub.
- Zero or more spokes may be chosen, but at most
kof them. - Every spoke must be a distinct node that shares an edge with the hub.
- A hub with no spokes is a valid selection.
- Each chosen node (the hub and every spoke) contributes its value exactly once to the total.
- Edges between two spokes are irrelevant. You only need the hub-to-spoke edges, and any extra edges among chosen nodes are simply ignored.
Task
Implement max_hub_spoke_sum(num_nodes, edge_from, edge_to, values, k).
num_nodesis the number of nodes, labeled0tonum_nodes - 1.edge_fromandedge_tohave the same length. Thei-th edge connectsedge_from[i]andedge_to[i].values[i]is the value of nodei.kis the maximum number of spokes allowed.
Return a single integer: the maximum total value over all valid selections. Since a selection always contains at least the hub, the result is never an empty sum. If every value is negative, the answer is the best single node value, not 0.
Constraints
2 <= num_nodes <= 1000001 <= len(edge_from) = len(edge_to) <= 100000- All edge endpoints are valid node labels.
- No self-loops, and no pair of nodes is joined by more than one edge.
len(values) = num_nodesand-1000 <= values[i] <= 10000 <= k <= 100000- The graph may be disconnected.
Examples
Example 1
Input: max_hub_spoke_sum(4, [0, 0, 0], [1, 2, 3], [5, 4, -2, 3], 2)
Output: 12
Example 2
Input: max_hub_spoke_sum(2, [0], [1], [-4, -1], 1)
Output: -1
Hints
Hint 1
Every valid selection has exactly one hub. What happens if you fix the hub and ask for the best set of spokes only for that hub?
Hint 2
Once the hub is fixed, the spokes are independent of each other: each neighbor just adds its own value. Which neighbors should you never take, given that fewer than k spokes is allowed?
Hint 3
For each hub, sum the k largest strictly positive neighbor values and add the hub's own value. Take the maximum over all hubs, and do not start your running best at 0 because the all-negative case must return a negative number.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Every valid selection has exactly one hub, so we try each node as the hub and compute the best selection around it. The answer is the maximum over all hubs.
For a fixed hub h, the total is values[h] plus the values of the chosen spokes. Spokes are chosen only from the neighbors of h, and each one contributes its own value regardless of the others (edges among spokes do not matter). So the choice reduces to:
- Discard neighbors whose value is
<= 0. Since "at mostk" spokes are allowed, leaving them out never lowers the total. - Among the remaining positive neighbors, take the
klargest (or all of them if there are fewer thank). - The candidate for this hub is
values[h]plus the sum of those values.
Finally, return the best candidate over all hubs.
Why it is correct
Take any valid selection with hub h and spoke set S, |S| <= k. Removing non-positive spokes from S does not decrease the sum and keeps the selection valid. What remains is a set of at most k positive neighbors. The greedy choice either contains all positive neighbors (when there are at most k) or the k largest ones, and exchanging any chosen element for a smaller one can only reduce the sum. So the greedy choice is at least as good as S. Since every selection has some hub and we consider all hubs, the overall maximum is correct.
Distinct spokes are guaranteed because the graph is simple, so each neighbor appears once in the adjacency list. Starting best at max(values) ensures the result is never an artificial 0: a lone hub is always valid, so with all-negative values the answer is the largest single value. When k = 0, the slice is empty and the answer is max(values).
Complexity
Let V = num_nodes and E be the number of edges.
- Time: building adjacency lists is
O(V + E). Sorting each hub's positive neighbors costsO(deg log deg), and the degrees sum to2E, so the total isO(V + E log E). - Space:
O(V + E)for the adjacency lists.
Reference solution
from typing import List
def max_hub_spoke_sum(num_nodes: int, edge_from: List[int], edge_to: List[int], values: List[int], k: int) -> int:
adj = [[] for _ in range(num_nodes)]
for u, v in zip(edge_from, edge_to):
adj[u].append(v)
adj[v].append(u)
best = max(values)
for hub in range(num_nodes):
gains = sorted(
(values[nb] for nb in adj[hub] if values[nb] > 0),
reverse=True,
)
total = values[hub] + sum(gains[:k])
if total > best:
best = total
return best