当前位置:网站首页>Depth first traversal template principle of tree and graph
Depth first traversal template principle of tree and graph
2022-07-07 04:38:00 【_ Liu Xiaoyu】
A tree is a special graph ( Acyclic connectivity )
So just know the picture
chart : It is divided into Directed graph and Undirected graph
Figure of the storage
Directed graph :(a–>b)
- Adjacency matrix g[][], The weight without weight is directly equal to bool, If there is a weight, assign the weight directly to it
- Adjacency list ( Store... In a linked list )( It can also be used. vector save , It's just a little inefficient )
Adjacency table storage code implementation :
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010, M = N * 2;
int n,m;
// h Express n A single chain watch
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);
}
Graph traversal
Depth-first traversal : One way to the end , Then look back to see if there are other nodes that have not been traversed
Depth first traversal code implementation :
#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);
}
边栏推荐
- A detailed explanation of head pose estimation [collect good articles]
- Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
- 接口自动化测试实践指导(中):接口测试场景有哪些
- AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
- Advertising attribution: how to measure the value of buying volume?
- How do test / development programmers get promoted? From nothing, from thin to thick
- 计数排序基础思路
- Fiance donated 500million dollars to female PI, so that she didn't need to apply for projects, recruited 150 scientists, and did scientific research at ease!
- How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
- SSM+jsp实现仓库管理系统,界面那叫一个优雅
猜你喜欢
英特尔David Tuhy:英特尔傲腾技术成功的原因
Camera calibration (I): robot hand eye calibration
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
kivy教程之设置窗体大小和背景(教程含源码)
Ssm+jsp realizes enterprise management system (OA management system source code + database + document +ppt)
Mathematical analysis_ Notes_ Chapter 10: integral with parameters
Ssm+jsp realizes the warehouse management system, and the interface is called an elegant interface
【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
EasyCVR集群重启导致其他服务器设备通道状态离线情况的优化
随机推荐
buildroot的根文件系统提示“depmod:applt not found”
Network Security Learning - Information Collection
Kivy tutorial of setting the size and background of the form (tutorial includes source code)
【实践出真理】import和require的引入方式真的和网上说的一样吗
mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
两个div在同一行,两个div不换行「建议收藏」
SSM+jsp实现仓库管理系统,界面那叫一个优雅
Vscode 如何使用内置浏览器?
未婚夫捐5亿美元给女PI,让她不用申请项目,招150位科学家,安心做科研!
过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
高薪程序员&面试题精讲系列120之Redis集群原理你熟悉吗?如何保证Redis的高可用(上)?
EasyCVR视频广场点击播放时,主菜单高亮效果消失问题的修复
You can't sell the used lithography machine to China! The United States unreasonably pressured the Dutch ASML, and domestic chips were suppressed again
Common methods of list and map
测试/开发程序员怎么升职?从无到有,从薄变厚.......
Different meat customers joined hands with Dexter to launch different hamburgers in some stores across the country
Lessons and thoughts of the first SQL injection
一图看懂!为什么学校教了你Coding但还是不会的原因...
How do test / development programmers get promoted? From nothing, from thin to thick