CodeOath
← All problems

Number of Connected Components in an Undirected Graph

Medium
graphsunion-find

You have a graph of n nodes labeled 0 to n - 1, and a list of undirected edges (each [a, b] means there's an edge between a and b).

Return the number of connected components in the graph.

Example:

Input: n = 5, edges = [[0,1],[1,2],[3,4]]
Output: 2