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 注意: 你可以 立即下载
#include <vector>
#include <unordered_map>
#include <queue>
#include <iostream>
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 an undirected graph.
  For example:
  0
  |
  1 ---> 2    4--->3
  Given n = 5 and edges = [[0,1], [1, 2], [3, 4]] return 2.
*/
// it is pretty straightforward to come up BFS algorithm.
// But the first task is to change the pair vector into graph node-edges form.
// we can use hashMap to do that. unordered_map<int, vector<int> > hashMap.
void BFS(unordered_map<int, vector<int> >& hashMap, int node, unordered_map<int, bool>& visited) {
  queue<int> nodes;
  nodes.push(node);
  while(!nodes.empty()) {
    int tmp = nodes.front();
    nodes.pop();
    if(visited[tmp]) continue;  // if visited, skip it.
    visited[tmp] = true;
    auto iter = hashMap.find(tmp);
    vector<int> edges = iter->second;
    for(int k = 0; k < edges.size(); k++) {
      nodes.push(edges[k]);
    }
  }
}
int findNumOfConnectedComponent(int n, vector< vector<int> >& edges) {
  unordered_map<int, vector<int> > hashMap;
  unordered_map<int, bool> visited;
  int count = 0;
  for(int i = 0;  i < edges.size(); ++i) {
    hashMap[edges[i][0]].push_back(edges[i][1]);
    hashMap[edges[i][1]].push_back(edges[i][0]);
  }
  auto iter = hashMap.begin();
  while(iter != hashMap.end()) {
    visited[iter->first] = false;
    iter++;
  }
  auto it = visited.begin();
  while(it != visited.end()) {
    if(!(it->second)) {
      BFS(hashMap, it->first, visited);
      count++;
    }
    it++;
  }
  return count;
}
int main(void) {
  vector< vector<int> > edges {
  {0, 1},
  {1, 2},
  {3, 4},
  {5, 6},
  {7, 7}};
  cout << findNumOfConnectedComponent(8, edges) << endl;
}
 

海上AI导航系统:技术架构与行业应用解析 计算机视觉与强化学习技术在海上导航系统中的应用正逐步改变传统航运业。通过多模态传感器融合技术,现代AI导航系统能够实现高精度的物体识别与实时路径规划,有效解决能见度依赖、决策延迟等核心痛点。以Orca AI的SeaPod系统为例,其结合了NVIDIA GeForce RTX 4070的边缘计算能力与多光谱协同识别技术,在恶劣环境下仍能保持稳定性能。这类系统不仅提升了航行安全性,还通过优化航线显著降低燃油消耗,在东京湾、马六甲海峡等繁忙水域展现出巨大价值。随着毫米波雷达融合方案的测试推进,未来海上导航将突破极 阅读详情

相关推荐

OpenGL绘制简单场景,实现旋转缩放平移和灯光效果

本项目实现了用OpenGL绘制一个简单场景,包含正方体、球体和网格,实现了物体的旋转、缩放、平移和灯光效果。附有项目完整代码,有详细注释。适合初学者熟悉opengl使用。

张竞成的博客 1万+

力扣(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

免费测试国外大模型Claude 3.7 Sonnet API:无需api key(秘钥)

Claude 3.7 Sonnet是Anthropic公司推出的第三代Claude系列AI模型中的中规模版本(2024年发布),在性能与效率间实现平衡。该模型基于改进的Transformer架构,支持128K上下文窗口,擅长复杂推理、数学计算和创意写作,在GSM8K数学基准测试中准确率达92%。其采用"宪法AI"伦理框架,通过多轮对抗训练减少有害输出,同时新增多模态理解能力(可解析图片/图表内容)。Claude 3.7 Sonnet是Anthropic推出的高效轻量级大模型,主打快速响应与成本优化。

Explinks的博客 1506

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

你可以假设在 edges 中不会出现重复的边。而且由于所以的边都是无向边,[0, 1] 与 [1, 0] 相同,所以它们不会同时在 edges 中出现。

OceanStar的博客 938

51单片机STC8G1K08驱动ws2812记录

使用51单片机控制17个2812灯,这里选择STC8芯片,最大频率可以达到35M,参考网上的各种文章案例,使用定时器做的时候实测做不到规格书要求的245-345ns,STC8G1K08不带PWM,所以只能使用nop来做,好在单片机在这里只有做编程灯的功能,省略测试过程,下面是程序,使用的是24MHz内部晶振 //设置一个灯珠 void SendOnePix(unsigned char *ptr) { unsigned char i,j; unsigned char temp; fo...

的博客 7328

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

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

Atticus的博客 974

基于ESP32与PCM5102的WiFi无损音频传输系统设计与实现

本文详细介绍基于ESP32与PCM5102的WiFi无损音频传输系统设计与实现。通过I2S接口直接传输PCM数据,避免SRC重采样,实现真正的HIFI级音质。涵盖硬件连接、软件架构、网络传输优化及实际音质对比,为音频爱好者提供高性价比的无损无线音频解决方案。

rnn9storyteller的博客 955

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

在实车上进行lio-sam测试

PS:将雷达和imu相对固定后,拆下来,原地晃啊晃,直到xyz进度达到百分百,参考gif。标定完成后,会在result文件夹下生成result.txt。

如果想成为中心,那么就到中心去吧。 1001

UG NX二次开发(C#)-外部模式-批量删除某些图层中的体对象和曲线对象

在 UG NX中,有时需要批量删除某文件夹下多个.prt文件中固定一些图层中的体对象和曲线对象,这时如果采用手动逐个打开模型,并选择针对的图层删除,即耗时也容易遗漏,因此采用外部模式来处理比较方便。本文介绍批量删除某些图层中的体对象和曲线对象。

GimiGimmy 3041

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

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

OceanStar的博客 1302

YOLO26 网络结构逐模块解析:Backbone/Neck/Head

本文解析了YOLO26目标检测网络的架构设计,重点介绍其在Backbone、Neck和Head三个核心组件的创新优化。Backbone采用改进的CSP模块与轻量级注意力机制,提升特征提取效率;Neck使用改进的BiFPN结构实现高效多尺度特征融合;Head采用完全解耦设计简化回归流程。文章详细分析了各模块的技术演进、实现细节和设计思想,通过流程图和代码示例展示了YOLO26的整体架构和关键组件实现。该研究对模型优化、定制开发和硬件部署具有重要指导意义。

一键难忘的博客 1272

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

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

[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 解题报告 (C++)

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

负雪明烛 1622

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【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

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 解题报告

题目链接: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

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

James Pan 2316

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 157: Read N Characters Given Read4
下一篇: LeetCode 32. Longest Valid Parentheses
ActiveCoder
博客等级 码龄11年 4粉丝 345原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值