217. Number of Connected Components in an Undirected Graph
MediumGraph
You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and an array edges where edges[i] = [a_i, b_i] indicates that there is an edge between a_i and b_i in the graph. Return the number of connected components in the graph.
Examples
Input: 5 [[0,1],[1,2],[3,4]]
Output: 2
Explanation: Nodes {0,1,2} form one component and {3,4} another; node counting via union-find gives 2.
Constraints
- 1 <= n <= 2000
- 0 <= edges.length <= 5000
- edges[i].length == 2
- 0 <= a_i, b_i < n
- a_i != b_i
- There are no repeated edges.
Loading...
Run checks all cases above. Submit evaluates all test cases.