【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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:
Input: n = 5 and edges = [[0, 1], [1, 2], [3, 4]]
0 3
| |
1 --- 2 4
Output: 2
Example 2:
Input: n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]]
0 4
| |
1 --- 2 --- 3
Output: 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.
题目大意
给定编号从 0 到 n-1 的 n 个节点和一个无向边列表(每条边都是一对节点),请编写一个函数来计算无向图中连通分量的数目。
解题方法
并查集
看到求联通分量的题,一般都可以用并查集。比如1101. The Earliest Moment When Everyone Become Friends。
只要把并查集背下来,这个题目基本直接写上去就好了。
C++代码如下:
class Solution {
public:
int countComponents(int n, vector<vector<int>>& edges) {
map_ = vector<int>(n, 0);
components = n;
for (int i = 0; i < n; ++i) {
map_[i] = i;
}
for (vector<int>& edge : edges) {
uni(edge[0], edge[1]);
}
return components;
}
int find(int a) {
if (a == map_[a])
return a;
return find(map_[a]);
}
void uni(int a, int b) {
int pa = find(a);
int pb = find(b);
if (pa == pb)
return;
map_[pa] = pb;
components --;
}
private:
vector<int> map_;
int components;
};
日期
2019 年 9 月 22 日 —— 熬夜废掉半条命
【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)的更多相关文章
- LeetCode 323. 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 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 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), ...
- 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 ...
- 323. Number of Connected Components in an Undirected Graph
算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...
- [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), ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 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), ...
- [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), ...
随机推荐
- 使用FastqCount统计fastq文件基本信息?
目录 1. FastqCount简介 2. 使用 3. 结果 1. FastqCount简介 快速实用小工具:FastqCount https://github.com/zhimenggan/Fast ...
- 如何鉴定全基因组加倍事件(WGD)
目前鉴定全基因组加倍(whole-genome duplication events)有3种 通过染色体共线性(synteny) 方法是比较两个基因组的序列,并将同源序列的位置绘制成点状图,如果能在点 ...
- Markdown-写作必备
Markdown--入门指南 导语: Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdow ...
- 巩固java第六天
巩固内容: HTML 空元素 没有内容的 HTML 元素被称为空元素.空元素是在开始标签中关闭的. <br> 就是没有关闭标签的空元素(<br> 标签定义换行). 在 XHTM ...
- REMI源安装php7.3
参考:https://blog.csdn.net/Phplayers/article/details/100901352 php5.6安装参考:https://www.cnblogs.com/Easo ...
- python格式化输出的两种方式对比
1.%符号方法和format()函数方法 2.对比: 1 print('我今年%d岁' %22.125) 2 print('我今年{0:f}'.format(22.125)) 3 #报错 4 #槽中类 ...
- CSS系列,三栏布局的四种方法
三栏布局.两栏布局都是我们在平时项目里经常使用的,今天我们来玩一下三栏布局的四种写法,以及它的使用场景. 所谓三栏布局就是指页面分为左中右三部分然后对中间一部分做自适应的一种布局方式. 1.绝对定位法 ...
- 01 nodejs MVC gulp 项目搭建
文本内容 使用generator-express创建nodejs MVC DEMO 使用gulp实时编译项目 npm安装二进制包,无须再编译wget https://nodejs.org/dist/v ...
- vue-cli4脚手架搭建三
组件传值 <script> import LunBo from "./LunBo"; export default { name: 'Home', components ...
- When do we pass arguments by reference or pointer?
在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...