当前位置:网站首页>leetcode:323. 无向图中连通分量的数目
leetcode:323. 无向图中连通分量的数目
2022-08-09 22:01:00 【OceanStar的学习笔记】
题目来源
题目描述
注意:
你可以假设在 edges 中不会出现重复的边。而且由于所以的边都是无向边,[0, 1] 与 [1, 0] 相同,所以它们不会同时在 edges 中出现。
class Solution {
public:
int countComponents(int n, vector<pair<int, int> >& edges){
}
};
题目解析
图
思路:
- 先构建邻接表
- 然后DFS:
- 给每个节点都有个flag标记其是否被访问过
- 如果出现了一个未被访问过的节点,那么++ans,然后通过邻接表遍历所有相邻的节点,并将它们标记为访问过
- 遍历完所有的连通节点后我们继续寻找下一个未访问过的节点,以此类推直至所有的节点都被访问过了
- 最终ans就是联通区域的个数
class Solution {
void dfs(std::vector<std::vector<int>> &g, std::vector<bool> &v, int i){
v[i] = true;
for (int j = 0; j < g[i].size(); ++j) {
if(v[g[i][j]] == false){
dfs(g, v, g[i][j]);
}
}
}
public:
int countComponents(int n, vector<pair<int, int> >& edges){
std::vector<std::vector<int>> g;
g.resize(n);
// 1、采用邻接表构建无向连通图
for(auto a : edges){
g[a.first].push_back(a.second);
g[a.second].push_back(a.first);
}
// 2、采用BFS获取连通分量个数
int ans = 0;
std::vector<bool> v(n, false);
for (int i = 0; i < n; ++i) {
if(v[i] == false){
dfs(g, v, i);
++ans;
}
}
return ans;
}
};
并查集
class Solution {
class UnionFind{
int cnt;
std::vector<int> parents;
int findRoot(int i){
int root = i;
while (parents[root] != root){
root = parents[root];
}
return root;
}
public:
UnionFind(int n){
cnt = n; // 初始时所有顶点都指向自己,连通分量数为节点数
parents.resize(cnt);
for (int i = 0; i < cnt; ++i) {
parents[i] = i; // 初始时所有节点都指向自己
}
}
int getConnected(){
return cnt;
}
void merge(int i, int j){
int p1 = findRoot(i);
int p2= findRoot(j);
if(p1 == p2){
return;
}
parents[p1] = p2;
--cnt;
}
};
public:
int countComponents(int n, vector<pair<int, int> >& edges){
UnionFind u(n);
// 遍历邻接表(边)合并连通节点
for (int i = 0; i < edges.size(); ++i) {
u.merge(edges[i].first, edges[i].second);
}
return u.getConnected();
}
};
类似题目
- LeetCode 133. Clone Graph 克隆无向图
- LeetCode 310. Minimum Height Trees(最小高度树)
- LeetCode 207. Course Schedule 课程清单
边栏推荐
- R语言ggstatsplot包grouped_ggscatterstats函数可视化分组散点图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)
- 【服务器数据恢复】SAN LUN映射出错导致文件系统数据丢失的数据恢复案例
- p5.js实现的炫酷星体旋转动画
- Liver all night to write a thirty thousand - word all the commands the SQL database, function, speaks clearly explain operators, content is rich, proposal collection + 3 even high praise!
- Rust 解引用
- SecureCRT background color
- 台风生成,广州公交站场积极开展台风防御安全隐患排查
- Leetcode.25 K个一组翻转链表(模拟/递归)
- This article lets you quickly understand implicit type conversion [integral promotion]!
- Kubernetes Service对象
猜你喜欢
随机推荐
2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day
C. Mere Array
R语言使用mean函数计算样本(观测)数据中指定变量的相对频数:计算时间序列数据中大于前一个观测值的观测值所占的比例总体的比例
【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
Converting angles to radians
Postgresql源码(68)virtualxid锁的原理和应用场景
unit test
每日一R「02」所有权与 Move 语义
为什么这么多人都想当产品经理?
【服务器数据恢复】SAN LUN映射出错导致文件系统数据丢失的数据恢复案例
R语言ggstatsplot包grouped_ggscatterstats函数可视化分组散点图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)
Jinshanyun earthquake, the epicenter is in bytes?
Flask之路由(app.route)详解
JuiceFS 在多云存储架构中的应用 | 深势科技分享
[Cloud Native] 4.2 DevOps Lectures
聊聊SQL语句中 DDL 、DML 、DQL 、DCL 分别是什么
5个 Istio 访问外部服务流量控制最常用的例子,你知道几个?
JS解混淆-AST还原案例
mysql 找不到或无法加载已注册的 .Net Framework Data Provider。