题目
并查集
class Solution {
public:
int Find[205];
int check(int k) {
if (k == Find[k]) {
return Find[k];
}
return Find[k] = check(Find[k]);
}
int findCircleNum(vector<vector<int>>& isConnected) {
int len = isConnected.size();
for (int i = 1; i <= 200; i++) {
Find[i] = i;
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (isConnected[i][j]) {
Find[check(j+1)] = check(Find[i+1]);
}
}
}
int res = 0;
for (int i = 1; i <= len; i++) {
if(i == check(i)) {
res++;
}
}
return res;
}
};