当前位置:网站首页>树与图的深度优先遍历模版原理
树与图的深度优先遍历模版原理
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);
}
边栏推荐
- Golang calculates constellations and signs based on birthdays
- 视频融合云平台EasyCVR视频广场左侧栏列表样式优化
- JS form get form & get form elements
- 别样肉客联手德克士在全国部分门店推出别样汉堡
- Network Security Learning - Information Collection
- Win11图片打不开怎么办?Win11无法打开图片的修复方法
- Golang compresses and decompresses zip files
- Zero knowledge private application platform aleo (1) what is aleo
- kivy教程之设置窗体大小和背景(教程含源码)
- How to write a resume that shines in front of another interviewer [easy to understand]
猜你喜欢
機器人(自動化)課程的持續學習-2022-
Have you got the same "artifact" of cross architecture development praised by various industry leaders?
Food Chem | in depth learning accurately predicts food categories and nutritional components based on ingredient statements
[OA] excel document generator: openpyxl module
Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
EasyCVR集群版本添加RTSP设备提示服务器ID错误,该如何解决?
mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展
Five years of automated testing, and finally into the ByteDance, the annual salary of 30W is not out of reach
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
随机推荐
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
Continuous learning of Robotics (Automation) - 2022-
[untitled]
Comment les tests de logiciels sont - ils effectués sur le site Web? Testez la stratégie!
Use facet to record operation log
Data security -- 12 -- Analysis of privacy protection
Win11 control panel shortcut key win11 multiple methods to open the control panel
Win11玩绝地求生(PUBG)崩溃怎么办?Win11玩绝地求生崩溃解决方法
Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
Intel David tuhy: the reason for the success of Intel aoten Technology
Implementation of JSTL custom function library
Web3 社区中使用的术语
JS form get form & get form elements
Opencv third party Library
Dab-detr: dynamic anchor boxes are better queries for Detr translation
[coded font series] opendyslexic font
一度辍学的数学差生,获得今年菲尔兹奖
EasyCVR视频广场点击播放时,主菜单高亮效果消失问题的修复
The first introduction of the most complete mongodb in history