forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0547.cpp
32 lines (30 loc) · 739 Bytes
/
0547.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
class Solution
{
public:
int findCircleNum(vector<vector<int>>& M)
{
vector<int> visited(M.size(), 0);
int res = 0;
for (int i = 0; i < M.size(); ++i)
{
if (!visited[i])
{
dfs(M, visited, i);
++res;
}
}
return res;
}
void dfs(vector<vector<int>>& M, vector<int>& visited, int i)
{
for (int j = 0; j < M.size(); ++j)
{
if (!visited[j] and M[i][j])
{
visited[j] = 1;
dfs(M, visited, j);
}
}
}
};