62. Number of Provinces

isConnected[i][j] = 1 means cities i and j are directly connected. A province is a group of cities connected directly or indirectly. Return the number of provinces.

Example 1

Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2

Example 2

Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
Hint 1

Count connected components.

Hint 2

Union-Find: start with n groups and decrease the count on every successful union.

Union FindGraph