当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

UI自动化测试从此告别手动下载浏览器驱动

DMX parameter exploration of grandma2 onpc 3.1.2.5
![[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

How to use jedis of redis

CTF stegano practice stegano 9
![[an Xun cup 2019] not file upload](/img/f1/736eb5fe51c299e3152ca87895ee99.png)
[an Xun cup 2019] not file upload
![[system security] ten thousand words summary system virtualization container bottom layer principle experiment](/img/c6/1bdb29a0acb0739f67b882fa6b3b47.jpg)
[system security] ten thousand words summary system virtualization container bottom layer principle experiment

Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
![[vérification sur le Web - divulgation du code source] obtenir la méthode du code source et utiliser des outils](/img/ea/84e67a1fca0e12cc4452c744c242b4.png)
[vérification sur le Web - divulgation du code source] obtenir la méthode du code source et utiliser des outils

Operation flow of UE4 DMX and grandma2 onpc 3.1.2.5
随机推荐
C # use awaiter
反絮凝剂-氨碘肽滴眼液
一文带你了解BI的前世今身与企业数字化转型的关系
[wp][入门]刷弱类型题目
Interview byte, pass the exam and directly work on three sides. As a result, I found an architect to hang me?
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘
【无标题】
Analysis of glibc strlen implementation mode
v-if VS v-show 2.0
Solve the problem that sqlyog does not have a schema Designer
grandMA2 onPC 3.1.2.5的DMX参数摸索
[software reverse - basic knowledge] analysis method, assembly instruction architecture
This article takes you to understand the relationship between the past and present of Bi and the digital transformation of enterprises
speed or tempo in classical music
Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
25K 入职腾讯的那天,我特么哭了
PlasticSCM 企业版Crack
An elegant program for Euclid‘s algorithm
speed or tempo in classical music