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.

分析:

  类似于图像处理中找到物体轮廓的泛洪算法。在本题中,每个点扫一遍,每条边扫一遍,点和边都不重复遍历,复杂度为O(V + E)

代码:

int number(int n, vector<pair<int, int> > edges) {
//Store edges with hash map for efficient access
unordered_multimap<int, int> hash;
for(auto e : edges) {
hash.insert(e);
hash.insert(make_pair(e.second, e.first));
}
//Flood fill algorithm
vector<bool> visited(n, false);
int number = ;
for(int i = ; i < n; i++) {
if(!visited[i]) {
number++;
visited[i] = true;
queue<int> myq;
myq.push(i);
while(!myq.empty()) {
auto range = hash.equal_range(myq.front());
myq.pop();
auto pos = range.first;
while(pos != range.second) {
if(!visited[pos->second]) {
myq.push(pos->second);
visited[pos->second] = true;
}
pos++;
}
}
}
}
return number;
}

[Locked] Number of Connected Components in an Undirected Graph的更多相关文章

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

  2. LeetCode Number of Connected Components in an Undirected Graph

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

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

  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. LeetCode 323. Number of Connected Components in an Undirected Graph

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

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

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

  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. 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。

    错误: 解决方案: 1."设置应用程序池默认属性"/"常规"/"启用32位应用程序",设置为 true. 如下图所示:(已测试,好使) 方法 ...

  2. angularjs-googleMap googleMap api地址解析与反解析

    1.js:根据地址得到经纬度var myplace=$scope.place;//获取输入的地址var geocoder = new google.maps.Geocoder();//创建geocod ...

  3. Hessian(C#)介绍及使用说明

    什么是Hessian? Hessian是Caucho开发的一种二进制Web Service协议.支持目前所有流行的开发平台. Hessia能干什么? hessian用来实现web服务. Hessia有 ...

  4. cmd运行java程序,无黑框闪烁

    程序目录中创建 “启动.bat” @echo off set mypath="%~dp0myjar.jar" echo %mypath% start javaw -jar %myp ...

  5. 【POJ2886】【线段树】Who Gets the Most Candies?

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

  6. PAT - 基础 - 龟兔赛跑

    题目: 乌龟与兔子进行赛跑,跑场是一个矩型跑道,跑道边可以随地进行休息.乌龟每分钟可以前进3米,兔子每分钟前进9米:兔子嫌乌龟跑得慢,觉得肯定能跑赢乌龟,于是,每跑10分钟回头看一下乌龟,若发现自己超 ...

  7. XAMPP 使用教程

        XAMPP 是一个把Apache网页服务器与PHP.Perl及MySQL集合在一起的安装包,允许用户可以在自己的电脑上轻易的建立网页服务器.使用 XAMPP 您可以轻松的在本机调试您的 PHP ...

  8. js操作Cookie,实现历史浏览记录

    /** * history_teacher.jsp中的js,最近浏览名师 * @version: 1.0 * @author: mingming */ $(function(){ getHistory ...

  9. iOS工程适配64-bit经验分享

    终究还是来了.Apple下发了支持64位的最后通牒: As we announced in October, beginning February 1, 2015 new iOS apps submi ...

  10. Ruby自学笔记(三)— 方法Method

    Ruby做为面向对象语言,肯定要对对象进行相关的操作,这时候就涉及到方法了. 调用方法 - 对象.方法名(实参1,实参2,...,实参n) 方法的分类: 1. 实例方法:顾名思义,就是由实例来调用的方 ...