-
Notifications
You must be signed in to change notification settings - Fork 0
/
547_friendCircle.cpp
72 lines (58 loc) · 1.4 KB
/
547_friendCircle.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
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
using namespace std;
void dfs(int person, vector<bool>& visited, vector<vector<int>>& M)
{
for (int j = 0; j != M.size(); ++j) {
if (M[person][j] == 1 && !visited[j]) {
visited[j] = true;
dfs(j, visited, M);
}
}
}
int findRoot(int x, vector<int>& vec)
{
return x == vec[x] ? x : findRoot(vec[x], vec);
}
int findCircleNum(vector<vector<int>>& M) {
int m = M.size();
if (m < 1) return 0;
// dfs
// vector<bool> visited(m, false);
// int count = 0;
// for (int i = 0; i != m; ++i) {
// if (!visited[i]) {
// dfs(i, visited, M);
// count++;
// }
// }
// Union & Find uf;
vector<int> vec(m, 0);
for (int i = 0; i != m; ++i) {
vec[i] = i;
}
int count = m;
for (int i = 0; i != m; ++i) {
for (int j = i + 1; j != m; ++j) {
if (M[i][j] == 1) {
int rootA = findRoot(i, vec);
int rootB = findRoot(j, vec);
if (rootA != rootB) {
count--;
vec[rootB] = rootA;
}
}
}
}
return count;
}
int main(int argc, char const *argv[])
{
vector<vector<int>> M = {
{1, 1, 0},
{1, 1, 0},
{0, 0, 1}
};
cout << "result = " << findCircleNum(M) << endl;
return 0;
}