Problem
You have n nodes labelled 0 to n - 1 and a list of undirected edges, where [a, b] joins a and b. Return the number of connected components: groups of nodes where every node can reach every other through edges.
Example 1
- Input
- n = 5, edges = [[0, 1], [1, 2], [3, 4]]
- Output
- 2
- Why
- {0,1,2} and {3,4}
Example 2
- Input
- n = 3, edges = []
- Output
- 3
- Why
- every node is alone
Constraints
1 <= n <= 20000 <= edges.length <= 50000 <= a, b < n and a != b