VISUALGO//PROBLEMS
number of provinces · graphs – dfsmedium
LeetCode #547
graph
012
city
·
count
·
stack
·
·
·
ELEMENTS
isConnected
Python
def findCircleNum(isConnected: list[list[int]]) -> int:
    n = len(isConnected)
    seen = [False] * n
    provinces = 0
    for start in range(n):
        if seen[start]:
            continue
        provinces += 1
        seen[start] = True
        stack = [start]
        while stack:
            city = stack.pop()
            for nxt in range(n):
                if isConnected[city][nxt] and not seen[nxt]:
                    seen[nxt] = True
                    stack.append(nxt)
    return provinces