-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path547FriendCircles.cpp
63 lines (58 loc) · 1.48 KB
/
547FriendCircles.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Created by Alaric on 2019-10-05.
//
//DFS
class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
int n = M.size();
vector<int> visited(n);
int ans = 0;
for (int i = 0 ; i < n ; ++i){
if (visited[i] == 0){
++ans;
dfs(M,visited,i);
}
}
return ans;
}
private:
void dfs(vector<vector<int>>& M , vector<int>& visited , int curRow){
visited[curRow] = 1;
for (int j = 0 ; j < M.size(); ++j){
if (M[curRow][j] == 1 && visited[j] == 0){
visited[j] = 1;
dfs(M , visited ,j);
}
}
return;
}
};
//Union-Find
class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
int n = M.size();
vector<int> p(n);
iota(begin(p) , end(p) , 0);
function<int(int)> find = [&](int x){
return p[x] == x ? x : p[x] = find(p[x]);
};
for (int i = 0 ; i < n ; ++i){
for (int j = i+1 ; j<n ; ++j ){
if (M[i][j] == 1){
int rootx = p[find(i)];
int rooty = p[find(j)];
if(rootx != rooty)
p[rooty] = rootx;
}
}
}
int ans = 0;
unordered_set<int> circles;
for (int i = 0 ; i< n; ++i){
circles.insert(find(i));
}
return circles.size();
}
};