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

模板匹配加速之金字塔分层搜索 图像金字塔是一种多分辨率图像表示方法,它通过一系列逐渐缩小或放大的图像来表示原始图像的不同尺度。高斯金字塔(Gaussian Pyramid):通过不断下采样(降采样)生成更小的图像。拉普拉斯金字塔(Laplacian Pyramid):通过高斯金字塔和差分操作生成一组残差图像。在模板匹配中,我们主要使用高斯金字塔来实现多尺度匹配。金字塔灰度模板匹配通过多尺度搜索策略,在保持较高准确率的同时显著提升匹配速度,尤其适用于:高分辨率图像处理实时检测系统移动端设备等资源受限场景。 阅读详情

原题网址: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), 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.

方法一:深度优先搜索+并查集。

public class Solution {
    private int components;
    private void find(int prev, int node, boolean[] visit, int[] roots, List<Integer>[] graph) {
        if (visit[node]) return;
        if (prev != -1) {
            int pid = root(roots, prev);
            int rid = root(roots, node);
            if (pid != rid) {
                roots[pid] = rid;
                components --;
            }
        }
        visit[node] = true;
        for(int i=0; i<graph[node].size(); i++) {
            find(node, graph[node].get(i), visit, roots, graph);
        }
    }
    public int countComponents(int n, int[][] edges) {
        components = n;
        boolean[] visit = new boolean[n];
        List<Integer>[] graph = new List[n];
        for(int i=0; i<graph.length; i++) graph[i] = new ArrayList<>();
        for(int[] edge: edges) {
            graph[edge[0]].add(edge[1]);
            graph[edge[1]].add(edge[0]);
        }
        int[] roots = new int[n];
        for(int i=0; i<n; i++) roots[i] = i;
        for(int i=0; i<n; i++) {
            find(-1, i, visit, roots, graph);
        }
        return components;
    }
    private int root(int[] roots, int id) {
        while (id != roots[id]) {
            roots[id] = roots[roots[id]];
            id = roots[id];
        }
        return id;
    }
}

方法二:使用并查集来标记图的连通分量。

public class Solution {
    private int root(int[] roots, int i) {
        while (roots[i] != i) i = roots[roots[i]];
        return i;
    }
    public int countComponents(int n, int[][] edges) {
        int count = n;
        int[] roots = new int[n];
        for(int i=0; i<n; i++) roots[i] = i;
        for(int i=0; i<edges.length; i++) {
            int r1 = root(roots, edges[i][0]);
            int r2 = root(roots, edges[i][1]);
            if (r1 != r2) {
                roots[r2] = r1;
                count --;
            }
        }
        return count;
    }
}

YOLOv8与UNetV2融合:一种面向目标检测与图像分割的混合方法【附核心代码】 我们提出了一种基于YOLOv8和UNetV2相结合的混合架构。该架构利用YOLOv8作为主干进行目标检测,同时在YOLOv8的特征提取过程中,引入UNetV2来进行图像分割任务的增强。在该架构中,YOLOv8负责大范围目标检测和位置预测,而UNetV2则在检测网络的特征图基础上,通过其高效的上下文信息提取和细节恢复能力,进一步优化分割任务。在本文中,我们提出了一种改进YOLOv8的方法,通过引入图像分割网络UNetV2来提高图像分割检测性能。 阅读详情

相关推荐

STM407ZET6+OV5640调试心得

最近一个项目用了STMF407ZET6 +OV5640,调试了好几天做些总结; STMF407ZET6 开发板用的是正点原子的开发板,OV5640摄像头用的是秉火摄像头; 这其中有一个大坑,开发板的DCMI接口跟摄像头的接口不一样,要重新用杜邦线进行连接; 本人是在正点原子的“”15,ATK-OV5640摄像头模块“”源码上进行修改的。

kingseacjh的专栏 8328

力扣(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的博客 1308

STM32_WS2811驱动

STM32的WS2811驱动

健身房 2846

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

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

Atticus的博客 974

数字IC后端PR阶段Innovus,ICC,ICC2修复short万能脚本分享

数字IC后端PR阶段Innovus,ICC,ICC2修复short万能脚本分享

weixin_37584728的博客 3483

MIA文献阅读 —— 深度学习在医学图像分析中的最新进展及临床应用【2024】_再现性;深度学习

coderVAEVAEVAE) (Kingma威林,2014)在概率性的工作方式学习观测数据之间的映射空间x∈Rmx∈R^mx∈Rm 和潜在的空间z∈Rnmnz∈Rn(m>>n)。作为一个潜变量模型,VAEVAEVAE制定这个问题作为观察样品的最大化对数似logpxlog∫px∣zpzdzpx∣zp (x | z)p(x∣z)可以很容易地使用神经网络建模,和pzp (z)

2401_84181704的博客 1245

Leetcode 323. 无向图中连通分量的数目

你有一个包含 n 个节点的图。给定一个整数 n 和一个数组 edges ,其中 edges[i] = [ai, bi] 表示图中 ai 和 bi 之间有一条边。第一步,构建维护变量。使用rootDict记录各个节点归属的集合的根节点。第二步,构建并查集中用于寻找到节点x所在集合的根节点的函数。第二步,构建维护变量。使用visited记录已经遍历过的点。第三步,遍历所有的边,根据并查集求联通分量个数。第三步,通过DFS计算连通分量的个数。返回 图中已连接分量的数目。第一步,使用邻接表构建无向图。

m0_51437455的博客 1053

保姆级教程:用TI IWR6843雷达板跑通3D人体追踪demo(附CCS/UniFlash避坑指南)

本文提供了一份详尽的TI IWR6843毫米波雷达开发板3D人体追踪demo实现指南,涵盖开发环境搭建、工程导入编译、固件烧录及可视化工具配置全流程。特别针对CCS和UniFlash使用中的常见问题给出解决方案,帮助开发者快速掌握3D People Tracking技术应用。

weixin_29159441的博客 521

leetcode323:无向图中的连通区域的个数

题目描述: 参考博客:https://www.cnblogs.com/grandyang/p/5166356.html 题目地址:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 代码实现: #方法一:并查集 n = 5 v = [[0, 1], [1, 2]...

wwwsctvcom的博客 1983

STM32F103ZET6【标准库函数开发】------04五个串口配置实战:从零到一构建多串口通信系统

本文详细介绍了STM32F103ZET6标准库函数开发中五个串口(USART/UART)的配置实战,从硬件引脚识别、时钟树配置到多串口系统整合技巧。通过具体代码示例和调试经验,帮助开发者快速构建稳定的多串口通信系统,适用于工业控制、物联网网关等应用场景。

weixin_29188997的博客 350

Fiori Elements Analytical List Page 标题区配置全攻略:从变体、过滤模式到 UI.KPI 的工程化落地

ALP标题区是Fiori Elements Analytical List Page的核心交互区域,承担变体管理、过滤模式切换、KPI展示等关键功能。本文系统解析了标题区的组成结构、可用能力及工程实现要点,包括:1)标题区包含过滤类型切换、全局KPI、变体管理等交互元素;2)详细说明视觉过滤与紧凑过滤的同步规则及实现方式;3)介绍OData V2下通过UI.KPI注解配置标题区KPI的方法;4)提供manifest.json配置示例和CDS注解写法。文章还对比了V2与V4版本的差异,为ALP开发提供实用指导

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 251

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

Time: 20190903 Type: Medium 题目描述 给定编号从 0 到 n-1 的 n 个节点和一个无向边列表(每条边都是一对节点),请编写一个函数来计算无向图中连通分量的数目。 示例 1: 输入: n = 5 和 edges = [[0, 1], [1, 2], [3, 4]] 0 3 | | 1 --- 2 4 输出: 2 示例...

Bing's Blog 6270

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【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...

负雪明烛 1622

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

[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

清空 1111

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的博客 734

【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...

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

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 324

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

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

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] 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

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

leetcode323. Number of Connected Components in an Undirected Graph

解法一bfs/** * @Author RenXintao * @Date 9/8/17 */class UndirectedGraphNode { int label; ArrayList<UndirectedGraphNode> neighbors; UndirectedGraphNode(int x) { label = x; ne

rxt2012kc的博客 537

[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. E

魔豆(Magicbean)的博客 874
上一篇: LeetCode 322. Coin Change(兑换硬币)
下一篇: LeetCode 324. Wiggle Sort II(摇摆排序)
jmspan
博客等级 码龄11年 99粉丝 431原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值