当前位置:网站首页>[to be continued] [depth first search] 547 Number of provinces

[to be continued] [depth first search] 547 Number of provinces

2022-07-05 05:16:00 lee2813

One 、 subject

Yes n Cities , Some of them are connected to each other , Others are not connected . If the city a And the city b Direct connection , And the city b And the city c Direct connection , So the city a And the city c Indirectly connected .

Province It's a group of directly or indirectly connected cities , There are no other cities in the group that are not connected .

To give you one n x n Matrix isConnected , among isConnected[i][j] = 1 It means the first one i Two cities and j The two cities are directly connected , and isConnected[i][j] = 0 The two are not directly connected .

Back in the matrix Province The number of .

Example 1:
 Insert picture description here

Input :isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output :2
Example 2:
 Insert picture description here

Input :isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output :3

Two 、 Answer key

3、 ... and 、 Code

原网站

版权声明
本文为[lee2813]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140623115485.html