当前位置:网站首页>树与图的深度优先遍历模版原理
树与图的深度优先遍历模版原理
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);
}
边栏推荐
- 未婚夫捐5亿美元给女PI,让她不用申请项目,招150位科学家,安心做科研!
- [written to the person who first published the paper] common problems in writing comprehensive scientific and Technological Papers
- [knife-4j quickly build swagger]
- Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
- Highly paid programmers & interview questions. Are you familiar with the redis cluster principle of series 120? How to ensure the high availability of redis (Part 1)?
- 2022 electrician cup question B analysis of emergency materials distribution under 5g network environment
- Practice Guide for interface automation testing (middle): what are the interface testing scenarios
- POJ training plan 2253_ Frogger (shortest /floyd)
- What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
- Easycvr cannot be played using webrtc. How to solve it?
猜你喜欢
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
kivy教程之设置窗体大小和背景(教程含源码)
图灵诞辰110周年,智能机器预言成真了吗?
EasyCVR无法使用WebRTC进行播放,该如何解决?
【自动化经验谈】自动化测试成长之路
C # use Siemens S7 protocol to read and write PLC DB block
MySQL forgot how to change the password
测试/开发程序员怎么升职?从无到有,从薄变厚.......
SSM+JSP实现企业管理系统(OA管理系统源码+数据库+文档+PPT)
mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
随机推荐
Pyqt5 out of focus monitoring no operation timer
科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
Master the secrets of software security testing methods, and pinch the security test report with your hands
True global ventures' newly established $146million follow-up fund was closed, of which the general partner subscribed $62million to invest in Web3 winners in the later stage
什么是Web3
The first introduction of the most complete mongodb in history
True Global Ventures新成立的1.46亿美元后续基金关账,其中普通合伙人认缴6,200万美元以对后期阶段的Web3赢家进行投资
掌握软件安全测试方法秘笈,安全测试报告信手捏来
Golang calculates constellations and signs based on birthdays
高薪程序员&面试题精讲系列120之Redis集群原理你熟悉吗?如何保证Redis的高可用(上)?
Golang compresses and decompresses zip files
Analysis on urban transportation ideas of 2022 Zhongqing cup C
Video fusion cloud platform easycvr video Plaza left column list style optimization
What is CGI, IIS, and VPS "suggested collection"
一度辍学的数学差生,获得今年菲尔兹奖
[system management] clear the icon cache of deleted programs in the taskbar
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
Both primary and secondary equipment numbers are 0
MySQL null value processing and value replacement