Loading...
Given n nodes (0-indexed) and a list of undirected edges, return the number of connected components in the graph.
Input: first line is n, second line is edges as a,b pairs separated by semicolons (empty if no edges).
5 0,1;1,2;3,4
2
5 represents the number of nodes in the graph, and the edges are given as 0,1;1,2;3,4, which can be split into pairs: (0,1), (1,2), and (3,4).0, 1, and 2, and another containing nodes 3 and 4.4 is connected to node 3, but there are no edges connecting the first group of nodes (0, 1, 2) to the second group (3, 4), resulting in 2 separate components.