当前位置:网站首页>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;
}
边栏推荐
- 反絮凝剂-氨碘肽滴眼液
- IronXL for .NET 2022.6
- Plasticscm enterprise crack
- 特殊版:SpreadJS v15.1 VS SpreadJS v15.0
- A brief introduction to the behavior tree of unity AI
- NEW:Devart dotConnect ADO.NET
- Clickhouse synchronization MySQL (based on materialization engine)
- DMX parameter exploration of grandma2 onpc 3.1.2.5
- Redis6-01nosql database
- provide/inject
猜你喜欢
![[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1](/img/eb/c944d4a71ce30589e71059ed848899.png)
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
![[array]566 Reshape the matrix - simple](/img/3c/593156f5bde67bd56828106d7bed3c.png)
[array]566 Reshape the matrix - simple

NEW:Devart dotConnect ADO. NET
![[web source code code code audit method] audit skills and tools](/img/7c/2c26578da084b3cd15d8f252b0e132.png)
[web source code code code audit method] audit skills and tools

ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)

Operation flow of UE4 DMX and grandma2 onpc 3.1.2.5

面试汇总:这是一份全面&详细的Android面试指南

线程基础知识

Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation

@Transactional 注解导致跨库查询失效的问题
随机推荐
[untitled]
Redis6-01nosql database
What is test development? Why do so many companies hire test developers now?
[brush questions] BFS topic selection
汇编-入门
在线SQL转Excel(xls/xlsx)工具
Nmap使用手册学习记录
CTF stegano practice stegano 9
[web source code code code audit method] audit skills and tools
NEW:Devart dotConnect ADO.NET
Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
Official announcement! The third cloud native programming challenge is officially launched!
线程基础知识
MySQL winter vacation self-study 2022 11 (10)
Deflocculant aminoiodotide eye drops
Containerd series - what is containerd?
Web components series (VII) -- life cycle of custom components
NEW:Devart dotConnect ADO. NET
[array]566 Reshape the matrix - simple