[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), ...
随机推荐
- 初试linux,cp、rm、mv、file、umask等命令粗略使用方法
ls --color=never 不要依據檔案特性給予顏色顯示: --color=always 顯示顏色 --color=auto 讓系統自行依據設定來判斷是否給予顏色 --full-time 以完整 ...
- 微信支付之获取openid
一.准备工具 不管开发什么,官方的文档应该是第一个想到的这里把官方文档贴出来:微信网页授权文档除此之外,我们还需要一个内网穿透的工具在开发环境下让微信能访问到我们的域名.我使用的是natapp.此类工 ...
- 前端Map封装源码
源于后台思路,简单封装了一下Map插件,方便以后使用. function Map() { this.elements = new Array(); //获取MAP元素个数 this.size = fu ...
- django-自定义文件上传存储类
文件储存API:https://yiyibooks.cn/xx/django_182/ref/files/storage.html 编写自定义存储系统:https://yiyibooks.cn/xx/ ...
- 独角兽估值30亿美金,我们聊聊RPA是什么
https://www.jianshu.com/p/397ecd238ffc 缩短法定工作时间,已成国际劳动立法趋势,全球政府都曾面对这样的议题,过往企业IT也在思考这件事,开发出更好的软件系统帮助员 ...
- Dubbo源码分析:ChannelHandler
背景 一个请求经过序列化二进制数据转化成对象之后.请求进入netty框架,netty框架经过业务处理把主动权转交给NettyHandler对象.NettyHandler进入ChannelHandler ...
- janusgraph-遍历图的语言
精确查询 语句含义 测试语句 执行时间 查询顶点标签为FALV的顶点数量 g.V().hasLabel('FALV').count() 2400s 查询顶点属性中id为19012201 clockWi ...
- mongoDB新增数据库
现在,如果我们想创建名为exampledb的数据库.只需运行以下命令并在数据库中保存一条记录.保存第一个示例后,将看到已创建新数据库. use tt 这样就创建了一个数据库,如果什么都不操作离开的话, ...
- Linux桌面最轻量的Dock之Plank介绍
官方的文档描述 Plank 是“这个星球上最简洁的 dock”.该项目的目的就是仅提供一个 dock 需要的功能,尽管这是很基础的一个库,却可以被扩展,创造其他的含更多高级功能的 dock 程序. 这 ...
- CSPS_113
这场还是Dybala的差点AK场 可是我T3伪了只拿了20分 如果这... T1 xjb猜了个结论就过对拍 T2 鸡还儿竖+贪心 T3 正着贪心一遍,被卡了,只有20分 可是如果反着再来亿遍 就会有5 ...