<p>There are <code>n</code> cities. Some of them are connected, while some are not. If city <code>a</code> is connected directly with city <code>b</code>, and city <code>b</code> is connected directly with city <code>c</code>, then city <code>a</code> is connected indirectly with city <code>c</code>.</p>

<p>A <strong>province</strong> is a group of directly or indirectly connected cities and no other cities outside of the group.</p>

<p>You are given an <code>n x n</code> matrix <code>isConnected</code> where <code>isConnected[i][j] = 1</code> if the <code>i<sup>th</sup></code> city and the <code>j<sup>th</sup></code> city are directly connected, and <code>isConnected[i][j] = 0</code> otherwise.</p>

<p>Return <em>the total number of <strong>provinces</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg" style="width: 222px; height: 142px;" />
<pre>
<strong>Input:</strong> isConnected = [[1,1,0],[1,1,0],[0,0,1]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg" style="width: 222px; height: 142px;" />
<pre>
<strong>Input:</strong> isConnected = [[1,0,0],[0,1,0],[0,0,1]]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 200</code></li>
	<li><code>n == isConnected.length</code></li>
	<li><code>n == isConnected[i].length</code></li>
	<li><code>isConnected[i][j]</code> is <code>1</code> or <code>0</code>.</li>
	<li><code>isConnected[i][i] == 1</code></li>
	<li><code>isConnected[i][j] == isConnected[j][i]</code></li>
</ul>

class UnionFind:
    def __init__(self, size):
        self.parent = [i for i in range(size)]
        self.rank = [0] * size

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def union_set(self, x, y):
        xset = self.find(x)
        yset = self.find(y)
        if xset != yset:
            if self.rank[xset] < self.rank[yset]:
                self.parent[xset] = yset
            elif self.rank[xset] > self.rank[yset]:
                self.parent[yset] = xset
            else:
                self.parent[yset] = xset
                self.rank[xset] += 1

class Solution:
    def findCircleNum(self, isConnected):
        # Get the number of cities (nodes)
        n = len(isConnected)
        # Initialize a visited set to track cities we have already processed
        visited = set()

        # Depth-First Search function
        def dfs(city):
            # Iterate over all cities
            for neighbor in range(n):
                # If city is connected to neighbor and neighbor is not visited
                if isConnected[city][neighbor] == 1 and neighbor not in visited:
                    # Mark neighbor as visited
                    visited.add(neighbor)
                    # Continue DFS for the connected neighbor
                    dfs(neighbor)

        # Initialize province count
        provinces = 0

        # Iterate over each city
        for city in range(n):
            # If the city is not visited, it means it's a new province
            if city not in visited:
                # Perform DFS for this city
                dfs(city)
                # After DFS, increment the province count
                provinces += 1

        return provinces
    """
    def findCircleNum(self, isConnected):
        n = len(isConnected)
        dsu = UnionFind(n)
        numberOfComponents = n

        for i in range(n):
            for j in range(i + 1, n):  # Avoid double-counting and self-loops
                if isConnected[i][j] == 1 and dsu.find(i) != dsu.find(j):
                    numberOfComponents -= 1
                    dsu.union_set(i, j)

        return numberOfComponents
    """
17800