题目地址: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:

  1. Input: n = 5 and edges = [[0, 1], [1, 2], [3, 4]]
  2. 0 3
  3. | |
  4. 1 --- 2 4
  5. Output: 2

Example 2:

  1. Input: n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]]
  2. 0 4
  3. | |
  4. 1 --- 2 --- 3
  5. 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++代码如下:

  1. class Solution {
  2. public:
  3. int countComponents(int n, vector<vector<int>>& edges) {
  4. map_ = vector<int>(n, 0);
  5. components = n;
  6. for (int i = 0; i < n; ++i) {
  7. map_[i] = i;
  8. }
  9. for (vector<int>& edge : edges) {
  10. uni(edge[0], edge[1]);
  11. }
  12. return components;
  13. }
  14. int find(int a) {
  15. if (a == map_[a])
  16. return a;
  17. return find(map_[a]);
  18. }
  19. void uni(int a, int b) {
  20. int pa = find(a);
  21. int pb = find(b);
  22. if (pa == pb)
  23. return;
  24. map_[pa] = pb;
  25. components --;
  26. }
  27. private:
  28. vector<int> map_;
  29. int components;
  30. };

日期

2019 年 9 月 22 日 —— 熬夜废掉半条命

【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)的更多相关文章

  1. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  2. [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), ...

  3. 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), ...

  4. 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 ...

  5. 323. Number of Connected Components in an Undirected Graph

    算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...

  6. [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), ...

  7. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  8. 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), ...

  9. [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), ...

随机推荐

  1. 使用FastqCount统计fastq文件基本信息?

    目录 1. FastqCount简介 2. 使用 3. 结果 1. FastqCount简介 快速实用小工具:FastqCount https://github.com/zhimenggan/Fast ...

  2. 如何鉴定全基因组加倍事件(WGD)

    目前鉴定全基因组加倍(whole-genome duplication events)有3种 通过染色体共线性(synteny) 方法是比较两个基因组的序列,并将同源序列的位置绘制成点状图,如果能在点 ...

  3. Markdown-写作必备

    Markdown--入门指南 导语: Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdow ...

  4. 巩固java第六天

    巩固内容: HTML 空元素 没有内容的 HTML 元素被称为空元素.空元素是在开始标签中关闭的. <br> 就是没有关闭标签的空元素(<br> 标签定义换行). 在 XHTM ...

  5. REMI源安装php7.3

    参考:https://blog.csdn.net/Phplayers/article/details/100901352 php5.6安装参考:https://www.cnblogs.com/Easo ...

  6. python格式化输出的两种方式对比

    1.%符号方法和format()函数方法 2.对比: 1 print('我今年%d岁' %22.125) 2 print('我今年{0:f}'.format(22.125)) 3 #报错 4 #槽中类 ...

  7. CSS系列,三栏布局的四种方法

    三栏布局.两栏布局都是我们在平时项目里经常使用的,今天我们来玩一下三栏布局的四种写法,以及它的使用场景. 所谓三栏布局就是指页面分为左中右三部分然后对中间一部分做自适应的一种布局方式. 1.绝对定位法 ...

  8. 01 nodejs MVC gulp 项目搭建

    文本内容 使用generator-express创建nodejs MVC DEMO 使用gulp实时编译项目 npm安装二进制包,无须再编译wget https://nodejs.org/dist/v ...

  9. vue-cli4脚手架搭建三

    组件传值 <script> import LunBo from "./LunBo"; export default { name: 'Home', components ...

  10. When do we pass arguments by reference or pointer?

    在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...