Problem
You are given n nodes and a set of undirected edges (connections). Support connectivity operations: answer whether two nodes lie in the same connected component, and report the number of connected components. Some variants add edges incrementally and interleave connectivity queries.
Input / Output
n and a list of edges (pairs of nodes); optionally a sequence of connected(a, b) queries.a and b are connected; and/or the final count of connected components.Constraints
Example