当前位置:网站首页>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 课程清单
边栏推荐
- 在“企业通讯录”的盲区,融云的边界与分寸
- navicat 快捷键
- “稚晖君”为2022昇腾AI创新大赛打call&nbsp;期待广大开发者加入
- 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!
- Solution: Edu Codeforces 109 (div2)
- C. Mere Array
- js十五道面试题(含答案)
- R语言修改dataframe数据列的名称:使用dplyr包的rename函数修改列名、使用colnmaes函数修改列名、在数据筛选的时候重命名列名
- [Microservice~Nacos] Configuration Center of Nacos
- Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
猜你喜欢
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
Presto Event Listener开发
leetcode 39. 组合总和(完全背包问题)
开发者必备:一文快速熟记【数据库系统】和【软件开发模型】常用知识点
小程序+自定义插件的关键性
Synchronization lock synchronized traces the source
Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
Socket发送缓冲区接收缓冲区快问快答
17-GuliMall 搭建虚拟域名访问环境
MySQL——JDBC
随机推荐
SecureCRT background color
nvm下node安装;node环境变量配置
Deceptive Dice
小程序+自定义插件的关键性
Cesium渐变色3dtiles白模(视频)
String hashing (2014 SERC J question)
Use convert_to_tensor in Tensorflow to specify the type of data
JS–比想象中简单
从产品角度看 L2 应用:为什么说这是一个游乐场?
Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
Kubernetes Service对象
级联下拉菜单的实现「建议收藏」
Evolution of MLOps
POWER SOURCE ETA ETA Power Repair FHG24SX-U Overview
开发者必备:一文快速熟记【数据库系统】和【软件开发模型】常用知识点
Under the NVM node installation;The node environment variable configuration
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
第十七期八股文巴拉巴拉说(数据库篇)
C 在函数声明前加typedef
重要的不是成为海贼王,而是像路飞一样去冒险