Imagine you're analyzing a social network where each person has a value (representing their influence or importance), and you want to find the most valuable "star-shaped" community.
You're given an undirected graph with n nodes numbered from 0 to n-1, where each node i has a value vals[i]. The graph is defined by edges connecting these nodes.
A star graph is a special subgraph where:
- There's one center node
- All other nodes in the star are directly connected to this center
- No edges exist between the non-center nodes
The star sum is the total value of all nodes in the star (center + its neighbors). Your goal is to find the maximum possible star sum when the star contains at most k edges.
Key insight: Since we want to maximize the sum, for each potential center node, we should greedily select its k highest-valued neighbors (if they have positive values).
Input & Output
Constraints
- 1 โค n โค 105
- 0 โค edges.length โค min(2 ร 105, n ร (n - 1) / 2)
- edges[i].length == 2
- 0 โค ai, bi โค n - 1
- ai โ bi
- There are no repeated edges
- -104 โค vals[i] โค 104
- 0 โค k โค n - 1