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:

     |          |
---

Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.

Example 2:

     |           |
--- ---

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.

思路:并查集(Union find)

 class Solution {
public:
int getFather(vector<int>& father, int i) {
if (father[i] == i) return i;
father[i] = getFather(father, father[i]);
return father[i];
}
void merge(vector<int>& father, int i, int j) {
int fatherI = getFather(father, i);
int fatherJ = getFather(father, j);
father[fatherJ] = fatherI;
}
int countComponents(int n, vector<pair<int, int>>& edges) {
vector<int> father;
for (int i = ; i < n; i++) father.push_back(i);
for (int i = , n = edges.size(); i < n; i++)
merge(father, get<>(edges[i]), get<>(edges[i]));
unordered_set<int> unions;
for (int i = ; i < n; i++) unions.insert(getFather(father, i));
return unions.size();
}
};

Number of Connected Components in an Undirected Graph -- LeetCode的更多相关文章

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

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

  3. LeetCode Number of Connected Components in an Undirected Graph

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

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

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

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

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

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

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

  9. 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

随机推荐

  1. C编译器MinGW安装、下载及在notepad++中运行C程序

    一.C编译器MinGW的下载及安装步骤 打开MinGW官网:http://www.mingw.org/ 图一 图二 图三 图四 图五 图六 系统中配置环境变量: 图七 验证是否安装成功: CMD中运行 ...

  2. AppCan试用体验

    最近自己想开发一个基于Android平台的小应用,但不想使用JAVA开发,还要快速实现功能,学习成本低. 所以找了很多框架,最后基本锁定在phoneGap和AppCan,又看了AppCan与phone ...

  3. 架构师入门ing

    算法竞赛水平一般,算法工程师估计遥遥无期,准备开始架构方面的学习. 单纯依靠垂直提升硬件性能来提高系统性能的时代已结束,分布式开发的时代实际上早已悄悄地成为了时代的主流. 在一个团队里,架构师充当了技 ...

  4. Java TCP通信概念及实例

    TCP/UDP 协议 通俗解释: TCP协议和UDP协议的区别类似于电话系统和邮政系统. <1>TCP:类似于电话系统,建立双向的通信通道,确定连接,话音顺序接听. <2>UD ...

  5. 重复造轮子系列--字符串处理(C语言)

    这些字符代码是以前写的,源于很久很久以前的一个VC++项目,在当时的部门编程比赛里因为用了项目代码的xsplit函数,万万没想到,那个做了几年的项目里面居然有坑..xsplit函数居然不能split连 ...

  6. PHP命名空间与use

    当在一个大型项目很多程序员书写模板时,最怕出现的问题就是命名,如果一个PHP脚本出现了同名的类或者方法,就会报错(fatal error),使用命名空间可以 解决这个问题 知识点: 命名空间names ...

  7. KNN算法在保险业精准营销中的应用

    版权所有,可以转载,禁止修改.转载请注明作者以及原文链接. 一.KNN算法概述 KNN是Machine Learning领域一个简单又实用的算法,与之前讨论过的算法主要存在两点不同: 它是一种非参方法 ...

  8. [CF482B]Interesting Array

    题目大意:构造一个序列$S$,有$m$条限制,每条为$l\;r\;q$,表示$\&_{i=l}^r S_i=q$ 题解:每条限制就把$[l,r]$内的数或上$q$,最后判断就行了 卡点:我又写 ...

  9. 【距离GDOI:130天】 AC自动机ing

    弄完后缀数组,终于能安心来复习AC自动机了..其实当时学的很不好,非常不好..模版都是有问题的...今天花了第一节晚修和一节自习算是把AC自动机的基础弄好了...切掉3道基础题,然后就被某道坑爹题坑掉 ...

  10. code forces 990C

    http://codeforces.com/contest/990/problem/C C. Bracket Sequences Concatenation Problem time limit pe ...