79. Connected Components
Medium
A network has n vertices numbered from 0 to n - 1 and undirected connections. Each connection [a,b] joins two vertices. Return how many connected groups exist, including isolated vertices.
Example 1
Input: n = 6, conexoes = [[0,1],[1,2],[3,4]]
Output: 3
Example 2
Input: n = 5, conexoes = [[0,1],[1,2],[2,3],[3,4]]
Output: 1
Constraints
- 0 <= n <= 100000
- 0 <= conexoes.length <= 200000
- As pontas de cada conexão são válidas.
GraphsUnion-find
Saved in this browser
Caso 1
n =
6
conexoes =
[[0,1],[1,2],[3,4]]
Expected =
3
Caso 2
n =
5
conexoes =
[[0,1],[1,2],[2,3],[3,4]]
Expected =
1
Run checks these 2 cases. Submit runs all 6, including hidden ones.