[Leetcode] 323. Number of Connected Components in an Undirected Graph 解题报告

leetcode323. 无向图中连通分量的数目 给定编号从 0 到 n-1 的 n 个节点和一个无向边列表(每条边都是一对节点),请编写一个函数来计算无向图中连通分量的数目。 示例 1: 输入: n = 5 和 edges = [[0, 1], [1, 2], [3, 4]]      0          3      |          |      1 — 2    4  输出: 2 示例 2: 输入: n = 5 和 edges = [[0, 1], [1, 2], [2, 3], [3, 4]]      0           4      |           |      1 — 2 — 3 输出:  1 注意: 你可以 立即下载

题目:

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.

思路:

一道难度适中的Union-Find题目。这类题目也有套路:首先为每个顶点初始化一个单独的集合;然后遍历,每次遇到一个边,就把边的两个顶点所属的集合进行合并,同时总的连通图数量减1。注意判断两个顶点是否属于同一个集合的经典代码,个人感觉应该背下来^_^。

这道题目用BFS和DFS也可以求解,但代码要相对复杂一些。

代码:

class Solution {
public:
    int countComponents(int n, vector<pair<int, int>>& edges) {
        vector<int> parents(n);
        for (int i = 0; i < n; ++i) {
            parents[i] = i;
        }
        int ret = n;
        for (int i = 0; i < edges.size(); ++i) {
            int par1 = edges[i].first, par2 = edges[i].second;
            while (par1 != parents[par1]) {
                par1 = parents[par1];
            }
            while (par2 != parents[par2]) {
                par2 = parents[par2];
            }
            if (par1 != par2) {
                parents[par2] = par1;
                --ret;
            }
        }
        return ret;
    }
};
【论文复现】自动化细胞核分割与特征分析 HoVer-Net是一种精心设计的多分支深度学习网络架构,专为处理复杂的多组织显微图像中的细胞核实例分割与精确分类任务而开发。该网络通过创新的策略,不仅有效解决了细胞核在密集聚集区域难以准确区分的问题,还实现了对每个独立细胞核类型的细致分类。此外,HoVer-Net还引入了专门的上采样分支,这一设计旨在对每个成功分割出的细胞核实例进行进一步的核类型分类。这一分支利用高级语义信息,结合精心设计的分类策略,能够准确判断每个细胞核的所属类型,为医学研究提供了宝贵的细胞水平信息。 阅读详情

相关推荐

用MCP扩展Cursor的5种神奇用法:从文件操作到网页抓取实战

本文深入探讨了如何利用MCP(Model Context Protocol)扩展Cursor AI编程助手,解锁五种高效实用场景。从文件系统操作、网页内容抓取,到数据库查询、Figma设计稿对接,乃至构建自定义MCP服务器,文章通过实战案例展示了如何将Cursor从代码编辑器升级为能理解并操作工作环境的智能助手,显著提升开发效率与自动化水平。

weixin_29231027的博客 315

力扣(LeetCode)323. 无向图中连通分量的数目(2022.11.20)

链接:https://leetcode.cn/problems/number-of-connected-components-in-an-undirected-graph。给定一个整数 n 和一个数组 edges ,其中 edges[i] = [ai, bi] 表示图中 ai 和 bi 之间有一条边。输入: n = 5, edges = [[0,1], [1,2], [2,3], [3,4]]输入: n = 5, edges = [[0, 1], [1, 2], [3, 4]]

ChaoYue_miku的博客 1305

DRAM架构演进与HBM技术在高性能计算中的应用

DRAM(动态随机存取存储器)是现代计算系统中关键的内存技术,其架构优化直接影响数据供给能力。随着AI训练和科学计算等数据密集型负载的普及,传统DDR架构面临带宽墙、能效比和访问粒度等挑战。HBM(高带宽内存)通过3D堆叠技术(如TSV硅通孔)和异构集成(如PIM存内计算)显著提升了带宽和能效比。这些技术在高性能计算、AI推理和大模型训练等场景中展现出巨大潜力,例如HBM3的带宽可达819GB/s,能效比低至0.95pJ/b。未来,DRAM技术将继续向更高密度、更低功耗和更智能的内存计算方向发展。

weixin_35757191的博客 400

Number of Connected Components in an Undirected Graph

Givennnodes labeled from0ton - 1and 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:

leetcode 解题思路 938

感应加热电源设计避坑指南:半桥LC串联谐振的5个关键问题

本文深入解析半桥LC串联谐振在感应加热电源设计中的5个关键问题,包括均压电阻选型、功率管驱动时序、谐振频率补偿等实战经验。通过真实故障案例和解决方案,帮助工程师避开常见设计陷阱,提升电路稳定性和效率,特别适合从事桥感应加热电源开发的工程师参考。

weixin_27013291的博客 509

Cube生成定时器2触发双ADC同步采集并用DMA传输

前言 用cube生成一个用定时器触发ADC1,ADC2同步采集的程序,单片机选择的是STM32L476RGT6,用定时器2进行ADC采集触发,更改定时器2的定时周期便可以更改ADC的采样周期,ADC1和ADC2使用同步规则模式,并用DMA进行数据的传输。 STM32的ADC采样完成总共需要的时间是 ADC完成采样时间=采样周期+12个转换周期 举个例子,假如ADC的时钟是15MHz,采样周期是3个周期,3个采样周期加上12个转换周期,一共是15个周期,因为时钟是15MHz,所以完成一次ADC转换总共需要的时

一行代码的博客 4513

Leetcode—323.无向图中连通分量的数目【中等】Plus

测试结果 之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

Atticus的博客 973

订餐平台餐厅信息抓取:使用Python爬虫获取商家信息和用户评价

在这篇博客中,我们展示了如何使用Python爬虫抓取订餐平台上的餐厅信息和用户评价数据。通过使用requests和Selenium,我们能够有效地从静态和动态网页中提取数据,并使用pandas和matplotlib对数据进行存储和分析。未来,您可以根据需求进一步扩展功能,例如增加错误处理、增加抓取多个页面的能力,或者使用高级技术如分布式爬虫来抓取更大规模的数据。

2201_76125261的博客 1651

[Locked] Number of Connected Components in an Undirected Graph

Number of Connected Components in an Undirected Graph Givennnodes labeled from0ton - 1and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of conne...

weixin_30530939的博客 134

图像分割 - 使用Mask R-CNN模型在COCO数据集上进行图像分割任务

图像分割是计算机视觉领域的一个重要任务,它旨在将图像中的每个像素分类为对象或背景。Mask R-CNN是一种基于区域的卷积神经网络,可用于图像分割任务。它是Faster R-CNN的扩展,将目标检测与图像分割结合起来,能够同时检测和分割对象。在本博客中,我们将使用Mask R-CNN模型在COCO数据集上进行图像分割任务。我们将介绍如何下载和准备COCO数据集,以及如何使用Mask R-CNN模型进行训练和推理。

斌擎科技 1188

【图像去噪】论文复现:CLIP用于图像去噪提升泛化性!CLIPDenoising的Pytorch源码复现,跑通CLIPDenoising全流程,图文结合,网络结构梳理和拆解,对应源码注释!

论文题目:Transfer CLIP for Generalizable Image Denoising —— 用于可泛化图像去噪的转移CLIP。【图像去噪】论文精读:Transfer CLIP for Generalizable Image Denoising(CLIPDenoising)CLIPDenoising理论部分详解见上面对应的论文精读,本文以源码为主,理论部分只是粗略回顾。启发:多看其他领域的文章,分析是否能应用在去噪领域,这是产生idea的要素之一;

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 1412

leetcode:323. 无向连通图中的连通分量个数

建立一个root数组,下标和节点值相同,此时root[i]表示节点i属于group i,我们初始化了n个部分 (res = n),假设开始的时候每个节点都属于一个单独的区间,然后我们开始遍历所有的edge,对于一条边的两个点,他们起始时在root中的值不相同,这时候我们我们将结果减1,表示少了一个区间,然后更新其中一个节点的root值,使两个节点的root值相同,那么这样我们就能把连通区间的所有节点的root值都标记成相同的值,不同连通区间的root值不相同,这样也能找出连通区间的个数。

OceanStar的博客 1302

14、网络切片技术:原理、应用与挑战

本文详细探讨了网络切片技术的原理、应用场景及其在5G网络中的重要性。网络切片作为一种关键的网络架构技术,能够在同一物理网络上创建多个独立的逻辑网络,以满足不同应用对安全性、可靠性和性能的多样化需求。文章深入分析了网络切片在5G、SDN/NFV等技术背景下的发展与挑战,并探讨了其在制造业、交通运输、零售等多个行业的实际应用。此外,还介绍了网络切片的部署流程、未来发展趋势以及相关技术难点,为读者全面了解网络切片技术提供了有价值的参考。

palm99的专栏 253

【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法并查集日期 题目地址:https://leetcode-cn.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目描述 Given n nodes la...

负雪明烛 1617

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

[leetcode] 323. Number of Connected Components in an Undirected Graph 解题报告

题目链接:https://leetcode.com/problems/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), wr

小榕流光的专栏 1713

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Leetcode 323. Number of Connected Components in an Undirected Graph(python+cpp)

Leetcode 323. Number of Connected Components in an Undirected Graph题目解法1:DFS解法2:unionfind 题目 Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write ...

qq_37821701的博客 1825

[leetcode323]Number of Connected Components in an Undirected Graph

Question: 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

清空 1110

leetcode| 323. Number of Connected Components in an Undirected Graph(并查集/DFS/BFS)

题目 题目描述:一张含n节点的无向图,以及一组无向边,编写一个函数来查找无向图中连接的组件的数量。 Input: n = 5 and edges = [[0, 1], [1, 2], [3, 4]] 0 3 | | 1 --- 2 4 Output: 2 分析 解法DFS/BFS/并查集,DFS和BFS时间复杂度均为O...

mike_learns_to_rock的博客 768

LeetCode 323. Number of Connected Components in an Undirected Graph

#include #include #include #include using namespace std; /* Given n nodes labeled from 0 to n-1 and a list of undirected edges, write a function to find the number of connected components in a

ActiveCoder的博客 731

LeetCode 323. Number of Connected Components in an Undirected Graph(连通分量)

原题网址:https://leetcode.com/problems/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), wr

James Pan 2315

LeetCode323 Number of Connected Components in an Undirected Graph

Input: n = 5 and edges = [[0, 1], [1, 2], [3, 4]] 0 3 | | 1 --- 2 4 Output: 2 it is quite easy to understtandi this problem. now, how to solve it? if you really think about t...

Tech in Pieces 323

【Leetcode】323. Number of Connected Components in an Undirected Graph

题目地址: https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 求无向图的连通分量,可以假定没有平行边。可以用DFS求解,设一个visited数组,每遇到一个未访问过的节点就对其进行DFS,同时将搜索到的节点都标记为访问过;访问的同时统计连通分量的个数。思想上类似于floodfil...

数学、算法爱好者的博客 550

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: Inpu...

weixin_46442857的博客 267

LeetCode 323. Number of Connected Components in an Undirected Graph (Medium)

Description: 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. Ex...

weixin_45744426的博客 241

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:

883
上一篇: [Leetcode] 322. Coin Change 解题报告
下一篇: [Leetcode] 324. Wiggle Sort II 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值