Back to problems

Number of Provinces

Algorithm · Expedia · Medium

You are given an n x n matrix that describes how n cities are linked. An entry matrix[i][j] = 1 means city i and city j are directly connected; matrix[i][j] = 0 means they are not directly connected. A province is a group of cities where you can travel from any city to any other city using the direct connections (transitively). Your task is to count how many distinct provinces exist. Input Format The first line of standard input contains a single integer n — the number of…

Checking your access…