-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path547.cpp
37 lines (32 loc) · 1.05 KB
/
547.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
class Solution {
public:
int findCircleNum(vector<vector<int>>& isConnected) {
int n = isConnected.size();
vector <int> disjoint(n, -1);
int root_i, root_j;
int temp, temp2;
for (int i = 1 ; i < n ; i++){
for (int j = 0 ; j < i; j++){
if (isConnected[i][j] == 1){
root_i = disjoint[i] == -1 ? i : disjoint[i];
root_j = j;
while(disjoint[root_j] != -1){
temp = root_j;
root_j = disjoint[root_j];
disjoint[temp] = root_i;
}
if (root_i != root_j)
disjoint[root_j] = root_i;
}
}
}
int answer = 0;
for (int i = 0; i < n ; i++){
if (disjoint[i] == -1) {
//cout << i << endl;
answer += 1;
}
}
return answer;
}
};