当前位置:网站首页>DFS and BFS concepts of trees and graphs
DFS and BFS concepts of trees and graphs
2022-07-05 03:56:00 【Cherish forever】
1. Depth first traversal enters the recursive call itself every time you search
2. Width first traversal is not recursive, that is, search the next node that the current node can reach until the node is searched
Use Adjacency list
Traverse : Each point will be traversed only once
1. Depth-first traversal .
Search as deep as possible , Go back when you hit the bottom , Until you search all the points .
2. Breadth first traversal .
Search widely from the root node , Search all the points on the first floor every time .
void add(int a,int b) {
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int x) {
st[x]=true;
int size=0,sum=1;
for(int i=h[x]; i!=-1; i=ne[i]) {
int j=e[i];
if(st[j]) continue;
int s=dfs(j);
size=max(size,s);
sum+=s;
}
size=max(size,n-sum);
ans=min(size,ans);
return sum;
}
边栏推荐
- 在线SQL转Excel(xls/xlsx)工具
- Quick start of UI component development of phantom engine [umg/slate]
- 反絮凝剂-氨碘肽滴眼液
- [PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
- How to define a unified response object gracefully
- Redis之Jedis如何使用
- De debugging (set the main thread as hidden debugging to destroy the debugging Channel & debugger detection)
- [web Audit - source code disclosure] obtain source code methods and use tools
- Special Edition: spreadjs v15.1 vs spreadjs v15.0
- Why is there a reincarnation of 60 years instead of 120 years in the tiangan dizhi chronology
猜你喜欢
特殊版:SpreadJS v15.1 VS SpreadJS v15.0
ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
Redis之Jedis如何使用
为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
As soon as I write the code, President Wang talks with me about the pattern all day
【看完就懂系列】一文6000字教你从0到1实现接口自动化
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)
IronXL for . NET 2022.6
[array]566 Reshape the matrix - simple
[C language] address book - dynamic and static implementation
随机推荐
MindFusion. Virtual Keyboard for WPF
Special Edition: spreadjs v15.1 vs spreadjs v15.0
Deflocculant aminoiodotide eye drops
在线文本行固定长度填充工具
An elegant program for Euclid‘s algorithm
已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘
Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation
[learning notes] month end operation -gr/ir reorganization
【软件逆向-分析工具】反汇编和反编译工具
Flex flexible layout
IronXL for .NET 2022.6
汇编-入门
[positioning in JS]
PlasticSCM 企业版Crack
Why is there a reincarnation of 60 years instead of 120 years in the tiangan dizhi chronology
[array]566 Reshape the matrix - simple
【PHP特性-变量覆盖】函数的使用不当、配置不当、代码逻辑漏洞
Multimedia query
ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)