代码拉取完成,页面将自动刷新
class Solution {
public:
void depthFirstSearch(int src, set<int> &toDel, map<int, set<int>> &adjacency, bool *used) {
for (int to : adjacency[src]) {
if (used[to])
continue;
used[to] = true;
toDel.insert(to);
depthFirstSearch(to, toDel, adjacency, used);
// used[to] = false;
}
}
int findCircleNum(vector<vector<int>>& isConnected) {
bool *used = new bool[isConnected.size()];
for (int i = 0; i < isConnected.size(); i++)
used[i] = false;
set<int> nodes;
for (int i = 0; i < isConnected.size(); i++)
nodes.insert(i);
map<int, set<int>> adjacency;
for (int i = 0; i < isConnected.size(); i++)
for (int j = 0; j < isConnected.size(); j++)
if (isConnected[i][j])
adjacency[i].insert(j);
int group = 0;
while (!nodes.empty()) {
int src = *(nodes.begin());
used[src] = true;
set<int> toDel{ src };
depthFirstSearch(src, toDel, adjacency, used);
// used[src] = false;
for (int del : toDel)
nodes.erase(del);
group++;
}
delete[] used;
return group;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。