[LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.
Example 1:
0 3
| |
1 --- 2 4
Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.
Example 2:
0 4
| |
1 --- 2 --- 3
Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1.
Note:
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
这道题让我们求无向图中连通区域的个数,LeetCode中关于图Graph的题屈指可数,解法都有类似的特点,都是要先构建邻接链表Adjacency List来做。这道题的一种解法是利用DFS来做,思路是给每个节点都有个flag标记其是否被访问过,对于一个未访问过的节点,我们将结果自增1,因为这肯定是一个新的连通区域,然后我们通过邻接链表来遍历与其相邻的节点,并将他们都标记成已访问过,遍历完所有的连通节点后我们继续寻找下一个未访问过的节点,以此类推直至所有的节点都被访问过了,那么此时我们也就求出来了连通区域的个数。
解法一:
class Solution {
public:
int countComponents(int n, vector<pair<int, int> >& edges) {
int res = ;
vector<vector<int> > g(n);
vector<bool> v(n, false);
for (auto a : edges) {
g[a.first].push_back(a.second);
g[a.second].push_back(a.first);
}
for (int i = ; i < n; ++i) {
if (!v[i]) {
++res;
dfs(g, v, i);
}
}
return res;
}
void dfs(vector<vector<int> > &g, vector<bool> &v, int i) {
if (v[i]) return;
v[i] = true;
for (int j = ; j < g[i].size(); ++j) {
dfs(g, v, g[i][j]);
}
}
};
这道题还有一种比较巧妙的方法,不用建立邻接链表,也不用DFS,思路是建立一个root数组,下标和节点值相同,此时root[i]表示节点i属于group i,我们初始化了n个部分 (res = n),假设开始的时候每个节点都属于一个单独的区间,然后我们开始遍历所有的edge,对于一条边的两个点,他们起始时在root中的值不相同,这时候我们我们将结果减1,表示少了一个区间,然后更新其中一个节点的root值,使两个节点的root值相同,那么这样我们就能把连通区间的所有节点的root值都标记成相同的值,不同连通区间的root值不相同,这样也能找出连通区间的个数。
解法二:
class Solution {
public:
int countComponents(int n, vector<pair<int, int> >& edges) {
int res = n;
vector<int> root(n);
for (int i = ; i < n; ++i) root[i] = i;
for (auto a : edges) {
int x = find(root, a.first), y = find(root, a.second);
if (x != y) {
--res;
root[y] = x;
}
}
return res;
}
int find(vector<int> &root, int i) {
while (root[i] != i) i = root[i];
return i;
}
};
类似题目:
参考资料:
https://leetcode.com/discuss/77308/accepted-dfs-in-c
https://leetcode.com/discuss/77027/c-solution-using-union-find
https://leetcode.com/discuss/76519/similar-to-number-of-islands-ii-with-a-findroot-function
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数的更多相关文章
- [LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 323. Number of Connected Components in an Undirected Graph (leetcode)
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- Number of Connected Components in an Undirected Graph -- LeetCode
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [Locked] Number of Connected Components in an Undirected Graph
Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...
- 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集
[抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...
随机推荐
- 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(3): 抓取amazon.com价格
通过上一篇随笔的处理,我们已经拿到了书的书名和ISBN码.(网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(2): 抓取allitebooks.com书籍信息 ...
- 【十大经典数据挖掘算法】SVM
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART SVM(Support Vector ...
- Objective-C集合总结
Objective-C里面的集合主要包括:NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionar ...
- 高分辨率下IE浏览器缩放导致出现右侧滚动条问题的解决
0 问题描述 由于需要演示触控操作,采购了SurfacePro,SurfacePro的推荐分辨率为2736×1824,且默认缩放比例为200%,IE浏览器的默认缩放比例也是200%,这样就导致右侧出现 ...
- div+css3绘制基本图形
基本图形包括:矩形.圆角矩形.圆形.椭圆形.三角形.值线.弧 这些图形的绘制用到了CSS圆角属性,不考虑IE8. 下面的实现在chrome浏览器运行通过. 1.矩形 比较简单,通过CSS设置宽度.高度 ...
- 解决 Tomcat Server in Eclipse unable to start within 45 seconds 不能启动的问题
1.在 Eclipse 下方 Servers TAB页,双击 "Tomcat 7.0 at localhost": 2.在右上角处点开 Timeouts 的设定,修改Start( ...
- POJ-3032
算法 准备一个最多能存放13个元素的队列,开始时队列为空. 1. 输入n. 3. 将n加入队列. 4. 令i从n到2执行: // 此时队列中有n-i+1个元素 将i-1加入到队列首部. // 此时队列 ...
- WCF入门教程2——创建第一个WCF程序
本节目标 掌握接口 理解契约式编程 创建宿主程序 创建客户端程序访问服务 什么是接口 认识一下接口 必须知道的接口特性 接口不可以被实例化(常作为类型使用) 实现类必须实现接口的所有方法(抽象类除外) ...
- entityframework学习笔记--009-使用原生sql语句操作数据
1 使用原生SQL语句更新--Database.ExecuteSqlCommand 假设你有一张如图9-1所示的Payment数据库表. 图9-1 1.1 实体类型: public class Pay ...
- Microsoft Dynamics CRM 解决数据大于5000时,页面上只能导出5000+数据。
页面显示: update [MSCRM_CONFIG].[dbo].[DeploymentProperties] set IntColumn=10000 --调整成10000+ 页面导出: 一.在 ...