[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 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的更多相关文章
- [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 ...
- [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), ...
- 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 ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 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), ...
- 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), ...
- [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), ...
- 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
随机推荐
- SQL Server 的远程连接(转载)
SQL Server默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQLServer2012数据库,需要确认以下环节: 1)如果是工作组环境,则需要使用SQL Server身份验证 ...
- sid超过8个字符处理步骤
服务端配置如下: [oracle@p3 admin]$ cat listener.ora # listener.ora Network Configuration File: /home/oracle ...
- redhat6.4 配置centos6 yum替换
1.卸载掉系统redhat自带的yum rpm -qa |grep yum |xargs rpm -e --nodeps 2 下载相关的centos yum插件 主要有python-inipa ...
- KAFKA分布式消息系统[转]
KAFKA分布式消息系统 转自:http://blog.chinaunix.net/uid-20196318-id-2420884.html Kafka[1]是linkedin用于日志处理的分布式消 ...
- [总结]RTMP流媒体技术零基础学习方法
本文主要总结一些我在学习RTMP流媒体技术过程中积累的经验.也为后来学习RTMP流媒体技术的人们一个参考.本文力图从简到难,循序渐进的介绍RTMP流媒体技术的方方面面,先从应用说起,逐步深化剖析相关工 ...
- ios strong weak 的区别 与 理解
先一句话总结:strong类保持他们拥有对象的活着,weak类他们拥有的对象被人家一牵就牵走,被人家一干就干死.(strong是一个好大哥所以strong,呵呵,weak是一个虚大哥所以weak,呵呵 ...
- c++primerplus(第六版)编程题——第5章(循环和关系表达式)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- java中jdk环境配置
配置java环境,俗称jdk环境 首先进入配置环境的目录下:右键鼠标我的电脑->属性->高级系统设置->环境变量,在对应的"系统变量"框下配置一下变量: 规范的配 ...
- C# ToString常用技巧总结
ToString是在开发中最常用的操作,其作用是将其它类型以字符串类型表示.例如: int i=2;i.ToString() //”2″Object o = new Object();o.ToStri ...
- 自定义Excel导出简易组件
1.组件原理 excel的数据存储是以xml格式存储的,所以导出Excel文件可以通过生成XML来实现.当然XML必须符合一定的格式要求. 2.组件实现 (1)新建类库文件“MyExcel” (2)添 ...