[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), 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.
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.
这道题和305. numbers of islands II 是一个思路,一个count初始化为n,union find每次有新的edge就union两个节点,如果两个节点(u, v)原来不在一个连通图里面就减少count并且连起来,如果原来就在一个图里面就不管。用一个索引array来做,union find优化就是加权了,每次把大的树的root当做parent,小的树的root作为child。
Java:
public class Solution {
public int countComponents(int n, int[][] edges) {
int count = n;
// array to store parent
init(n, edges);
for(int[] edge : edges) {
int root1 = find(edge[0]);
int root2 = find(edge[1]);
if(root1 != root2) {
union(root1, root2);
count--;
}
}
return count;
} int[] map;
private void init(int n, int[][] edges) {
map = new int[n];
for(int[] edge : edges) {
map[edge[0]] = edge[0];
map[edge[1]] = edge[1];
}
} private int find(int child) {
while(map[child] != child) child = map[child];
return child;
} private void union(int child, int parent) {
map[child] = parent;
}
}
Python:
# Time: O(nlog*n) ~= O(n), n is the length of the positions
# Space: O(n) class UnionFind(object):
def __init__(self, n):
self.set = range(n)
self.count = n def find_set(self, x):
if self.set[x] != x:
self.set[x] = self.find_set(self.set[x]) # path compression.
return self.set[x] def union_set(self, x, y):
x_root, y_root = map(self.find_set, (x, y))
if x_root != y_root:
self.set[min(x_root, y_root)] = max(x_root, y_root)
self.count -= 1 class Solution(object):
def countComponents(self, n, edges):
"""
:type n: int
:type edges: List[List[int]]
:rtype: int
"""
union_find = UnionFind(n)
for i, j in edges:
union_find.union_set(i, j)
return union_find.count
类似题目:
[LeetCode] 547. Friend Circles 朋友圈
[LeetCode] 200. Number of Islands 岛屿的数量
[LeetCode] 305. Number of Islands II 岛屿的数量 II
Find minimum number of people to reach to spread a message across all people in twitter
All LeetCode Questions List 题目汇总
[LeetCode] 323. 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 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), ...
- 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- 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 ...
- 323. Number of Connected Components in an Undirected Graph
算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 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), ...
- [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), ...
随机推荐
- [LeetCode] 0200. Number of Islands 岛屿的个数
题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...
- Linux CentOS7.x安装docker全过程
1.在安装docker之前,首先使用yum -y remove docker命令移除系统中已有的旧版本的docker yum -y remove docker 这里显示该系统没有安装过docker: ...
- python完成数组格式的请求参数的加密计算
#输入 '''order_id:31489 join_course[0][join_tel]:13130999882 join_course[0][join_name]:任学雨 join_course ...
- WinDbg常用命令系列---!cppexr
!cppexr 简介 !cppexr显示C++异常记录的内容. 使用形式 !cppexr Address 参数 Address指定要显示的C++异常记录的地址. 支持环境 Windows 2000 E ...
- 洛谷 P3183 [HAOI2016]食物链 题解
P3183 [HAOI2016]食物链 题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题现在给你n个物种和m条能量流动关系,求其中的食物链条数.物种的名称为从1到n编号M条能量流动关系形 ...
- 56、Spark Streaming: transform以及实时黑名单过滤案例实战
一.transform以及实时黑名单过滤案例实战 1.概述 transform操作,应用在DStream上时,可以用于执行任意的RDD到RDD的转换操作.它可以用于实现,DStream API中所没有 ...
- c语言博客作业03--循环结构
0.展示PTA总分 1.本章学习总结 1.1学习内容总结 循环语句 for语句: for( 表达式1; 表达式2; 表达式3 ) { // 需要执行的语句; } 其执行过程是:表达式 1 首先执行且只 ...
- Java-Long类型精度丢失问题
问题 今天碰到一个问题,后端需要返回给前端Long类型的id,前端收到的id会发生精度丢失. 测试代码:后端返回的值为344739147160346624 但是前端获取的值为: 解决办法 将返回的值转 ...
- Linux基础及常用指令
1.Linux目录结构 bin(usr/bin,user/local/bin) #存放常用指令,如cp.cat.chown等 sbin(usr/sbin,user/local/sbin) #高权限指令 ...
- Echarts 入门操作
Echarts具有丰富的图表,可以说是数据可视化的神器: 1.下载Echarts 到官网或者点击以下文字[下载Echarts]即可下载: ①官网下载地址:https://echarts.baidu.c ...