Given an integer n and undirected edges. Return the number of pairs (a, b) with a < b such that there is no path between them.
pairs = Σ s × (remaining - s)"With $n = 10^5$, checking all $\binom{n}{2} \approx 5 \times 10^9$ pairs leads to immediate Time Limit Exceeded. We must solve this in $O(V + E)$ linear time."
"Any node in a connected component of size $s$ can reach exactly all $s$ nodes in that component. It cannot reach the other $n - s$ nodes outside it."
"Maintain remaining = n. For each component of size $s$, multiply pairs += (long) s * (remaining - s), then decrement remaining -= s. This counts every pair exactly once!"
"Total unreachable pairs can exceed $2 \times 10^9$, so the result must use a 64-bit long to prevent 32-bit integer overflow."