Last Updated: March 28, 2026
The problem boils down to: given an undirected graph and a set of initially infected nodes, figure out which single node's removal from the initial set would leave the fewest total infected nodes after the malware finishes spreading.
The critical insight is that malware spreads across entire connected components. If a connected component contains at least one initially infected node, every node in that component will eventually be infected. So the question becomes: which initially infected node, when removed, would "save" the most nodes from infection?
Here's where it gets interesting. Removing an initially infected node only saves its component if that node is the sole initially infected node in that component. If a component has two or more nodes from initial, removing just one of them won't help because the other one will still infect the entire component. So the best candidate for removal is the node that's the only initially infected node in its component, and whose component is the largest.
2 <= n <= 300 → With n up to 300, the adjacency matrix has at most 90,000 entries. O(n^2) is perfectly fine, and even O(n^3) would be under 27 million operations.graph[i][j] == graph[j][i] → Undirected graph. We can use Union Find without worrying about edge direction.1 <= initial.length <= n → The initial set can be as large as the entire graph. Edge case: every node might be initially infected.The most direct approach: try removing each node from initial one at a time, simulate the malware spread using BFS, and count how many nodes end up infected. Pick the removal that yields the fewest infections.
For each candidate node to remove, we create a modified initial set (excluding that node), then run BFS from every remaining initially infected node to find all reachable nodes. The total number of reachable nodes is the infection count for that removal.
initial array (so we naturally handle tie-breaking by smallest index).removeNode in initial: a. Create a set of remaining infected nodes = initial minus removeNode.
b. Run BFS from all remaining infected nodes to count total infected nodes.
removeNode that resulted in the smallest infection count.We're running a full BFS for every node in initial, but the component structure doesn't change between removals. What if we computed the connected components just once?
Here's the key observation that makes this problem elegant: malware spreads across entire connected components. If any node in a component is initially infected, all nodes in that component become infected. So we only need to think at the component level.
The question becomes: for each connected component, how many nodes from initial does it contain?
initial, it stays clean regardless of what we remove.initial, removing that node saves the entire component from infection.initial, removing any one of them doesn't help because the others will still infect the whole component.So the optimal node to remove is the one that's the sole infected node in the largest component. If no node is uniquely responsible for infecting its component, every removal saves zero nodes, and we return the smallest index in initial.
The insight is that malware spreads through connected components completely. Once any node in a component is infected, all nodes are infected. So the only way removing a node from initial can save nodes is if that node was the sole reason its component was infected in the first place.
If a component has two infected nodes and you remove one, the other still infects the whole component. You saved nothing. But if a component has exactly one infected node, removing it means no node in that component starts with malware, and the component stays clean.
initial belong to it.initial: if it's the only initially infected node in its component, its "savings" equals the component size.The DFS approach works well but requires explicit stack management. Union Find can build the same component structure more elegantly.
Union Find is a natural fit for connected component problems. Instead of running DFS to discover components, we can scan the adjacency matrix and union nodes that are connected. After processing all edges, nodes in the same component share the same root. We can then query component sizes and count initial nodes per component in a single pass.
The logic is identical to Approach 2: find a node in initial that's the sole infected node in its component, and pick the one in the largest component. Union Find just gives us a more elegant way to find and track components.
graph[i][j] == 1 where i < j, union nodes i and j.initial, find its root (component representative).initial share each root. Also track each component's total size.initial (sorted). For each node whose component has exactly 1 initial node, its savings equals the component size.