当前位置:网站首页>树与图的深度优先遍历模版原理
树与图的深度优先遍历模版原理
2022-07-06 22:04:00 【_刘小雨】
树是一个特殊的图 (无环连通)
所以只用知道图就行了
图 : 又分为 有向图 和 无向图
图的存储
有向图:(a–>b)
- 邻接矩阵 g[][], 无权重直接等于bool, 有权重直接将权重赋值给它
- 邻接表 (用链表存储)(也可以用vector存,只是效率差一点)
邻接表的存储代码实现:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010, M = N * 2;
int n,m;
// h 表示n 个单链表
int h[N], e[M], ne[M], idx;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
int main()
{
meset(h, -1, sizeof h);
}
图的遍历
深度优先遍历: 一条路走到结束, 然后在回溯看有没有其他节点没有遍历
深度优先遍历代码实现:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010, M = N * 2;
int n,m;
int h[N], e[M], ne[M], idx;
bool st[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u)
{
st[u] = true;
for(int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if(!st[j]) dfs(j);
}
}
int main()
{
memset(h, -1, sizeof h);
}
边栏推荐
- Master the secrets of software security testing methods, and pinch the security test report with your hands
- JS form get form & get form elements
- EasyCVR视频广场点击播放时,主菜单高亮效果消失问题的修复
- Digital chemical plants realize the coexistence of advantages of high quality, low cost and fast efficiency
- 接口自动化测试实践指导(中):接口测试场景有哪些
- [team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp
- 機器人(自動化)課程的持續學習-2022-
- Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
- AI 落地新题型 RPA + AI =?
- The first introduction of the most complete mongodb in history
猜你喜欢
C#使用西门子S7 协议读写PLC DB块
[knife-4j quickly build swagger]
Common methods of list and map
Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
Network Security Learning - Information Collection
Ssm+jsp realizes enterprise management system (OA management system source code + database + document +ppt)
Win11图片打不开怎么办?Win11无法打开图片的修复方法
EasyCVR平台接入RTMP协议,接口调用提示获取录像错误该如何解决?
EasyCVR集群重启导致其他服务器设备通道状态离线情况的优化
随机推荐
两个div在同一行,两个div不换行「建议收藏」
[multi threading exercise] write a multi threading example of the producer consumer model.
Optimization of channel status offline of other server devices caused by easycvr cluster restart
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
EasyUI export excel cannot download the method that the box pops up
[coded font series] opendyslexic font
一图看懂!为什么学校教了你Coding但还是不会的原因...
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
[untitled]
2022 electrician cup a question high proportion wind power system energy storage operation and configuration analysis ideas
What is CGI, IIS, and VPS "suggested collection"
抖音或将推出独立种草社区平台:会不会成为第二个小红书
NFT meta universe chain diversified ecosystem development case
SSM+jsp实现仓库管理系统,界面那叫一个优雅
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
kivy教程之设置窗体大小和背景(教程含源码)
科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
[on automation experience] the growth path of automated testing
EasyCVR集群版本添加RTSP设备提示服务器ID错误,该如何解决?
True Global Ventures新成立的1.46亿美元后续基金关账,其中普通合伙人认缴6,200万美元以对后期阶段的Web3赢家进行投资