-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0547.cpp
More file actions
23 lines (23 loc) · 771 Bytes
/
0547.cpp
File metadata and controls
23 lines (23 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
private:
void connectProvince(vector<vector<int>> const &isConnected, vector<bool> &marks, int city) {
marks[city] = true;
for (int neighbor = 0; neighbor < isConnected.size(); neighbor++) {
if (isConnected[city][neighbor] && !marks[neighbor])
connectProvince(isConnected, marks, neighbor);
}
}
public:
int findCircleNum(vector<vector<int>> &isConnected) {
int n = isConnected.size();
vector<bool> marks(n, false);
int circleCount = 0;
for (int city = 0; city < n; city++) {
if (!marks[city]) {
connectProvince(isConnected, marks, city);
circleCount++;
}
}
return circleCount;
}
};