A hospital network operates tens of thousands of computers across multiple buildings (radiology, ICU, labs). During a ransomware incident, the security team needs to quickly estimate blast radius by identifying how many isolated LAN segments exist—groups of machines that can reach each other through direct or indirect connections.
You are given a snapshot of the physical network wiring as an undirected graph. Each computer is labeled 0..n-1, and each cable connects two computers bidirectionally.
Implement a function:
n: int — number of computersconnections: list[list[int]] — list of undirected edges where connections[i] = [a, b] indicates a cable between computers a and bint — the number of connected components in the network (i.e., the number of isolated groups of computers)A connected component is a maximal set of nodes where every node is reachable from every other node via one or more cables.
Input: n = 6, connections = [[0,1],[1,2],[3,4]]
Output: 3
Explanation:
{0,1,2} (0 connects to 2 through 1){3,4}{5} (no cables)Input: n = 4, connections = [[0,1],[1,2],[2,3]]
Output: 1
Explanation: All computers are chained together, so the entire network is one component.
1 <= n <= 2000000 <= connections.length <= 200000connections[i].length == 20 <= a, b < na != bn (avoid O(n^2)).