Graph Theory37 sections · 1633 units
Open in Course

LeetCode 547 Number of Provinces - Problem Statement

LeetCode 547 (DSU Version)

Problem: Number of Provinces Task: Given the connection matrix of nn cities, count connected components. This is DSU's bread and butter. Process all connections, union connected cities, and count how many distinct leaders remain.

Alternatively, start with count = nn and decrement on each successful union. The input is an adjacency matrix, so you check isConnected[i][j] for all pairs. Only process pairs where i<ji < j to avoid duplicate unions.