There are n cities numbered from 1 to n. Some pairs of cities are directly connected by roads. If city A is connected to city B, and city B is connected to city C, then city A and city C are also considered to be in the same group.
A province is a maximal group of cities that are directly or indirectly connected to each other.
You are given the connectivity information of all cities in the form of an adjacency matrix. Determine the total number of provinces.
n — the number of cities.n lines each contain n integers (0 or 1), where the j-th integer in the i-th row indicates whether city i and city j are directly connected.Print a single integer — the number of provinces.
1 ≤ n ≤ 2003
1 1 0
1 1 0
0 0 1
2
Cities 1 and 2 belong to the same province, while city 3 forms another province. Hence, the answer is 2.
3
1 0 0
0 1 0
0 0 1
3
No two different cities are directly connected, so each city forms its own province.
Expedia • Pending
Expedia • Pending
Teradata • Pending
Teradata • Pending