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. 4.实现简单的shell sed替换功能

    # -*- coding:utf-8 -*- # Author: JACK ZHAO # 程序1: 实现简单的shell sed替换功能 import sys #判断参数个数 if len(sys.a ...

  2. GCC特性之__init修饰解析 - kasalyn的专栏 - 博客频道 - CSDN.NET

    , GCC特性之__init修饰解析 - kasalyn的专栏 - 博客频道 - CSDN.NET.MathJax_Hover_Frame {border-radius: .25em; -webkit ...

  3. JDK从1.8.x升级到9.0.1后Tomcat 8.0.x不能启动

    目录 描述 具体环境情况 处理办法 描述 JDK在今年9月发布后,我们项目也打算测试升级使用JDK 9.在我将JDK升级成 JDK 9.0.1后,启动tomcat失败(黑框一闪就没了).具体失败信息如 ...

  4. jQuery选择器之元素选择器

    元素选择器:根据给定(html)标记名称选择所有的元素. 描述: $('element') 搜索指定元素标签名的所有节点,这是一个合集的操作.同样的也有原生方法getElementsByTagName ...

  5. 【bzoj3444】最后的晚餐 并查集

    题目描述 n个人排成一排,有m个条件,第i个条件要求ai和bi相邻,求方案数. 输入 输入有m+1行,第一行有两个用空格隔开的正整数n.m,如题所示.接下来的m行,每一行有两个用空格隔开的正整数,第i ...

  6. 【bzoj1270】[BeijingWc2008]雷涛的小猫 dp

    题目描述   输入 输出 样例输入 样例输出 8 题解 dp 设f[i][j]表示在第i棵树的j高度时最多吃到的柿子数. 那么只有两种可能能够到达这个位置:滑下来.跳下来. 滑下来直接用f[i][j+ ...

  7. [THUWC2017][bzoj5020] 在美妙的数学王国中畅游 [LCT+泰勒展开]

    题面 LOJ传送门 思路 这里很重要 它提示我们,把给定的三个函数泰勒展开,并用LCT维护每一项泰勒展开式的值,维护十几项就满足了题目的精度要求 我们考虑一个函数在0位置的泰勒展开 $f(x)=\su ...

  8. BZOJ 1036 [ZJOI2008]树的统计Count | 树链剖分模板

    原题链接 树链剖分的模板题:在点带权树树上维护路径和,最大值和单点修改 这里给出几个定义 以任意点为根,然后记 size (u ) 为以 u 为根的子树的结点个数,令 v 为 u 所有儿子中 size ...

  9. 洛谷 P2155 [SDOI2008]沙拉公主的困惑 解题报告

    P2155 [SDOI2008]沙拉公主的困惑 题目描述 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为\(1\)到\(N\)的阶乘,但是,政府只发行编号与\(M!\ ...

  10. 洛谷 P1503 鬼子进村 解题报告

    P1503 鬼子进村 题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. ...