当前位置:网站首页>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;
}
边栏推荐
- 函数基础学习02
- An elegant program for Euclid‘s algorithm
- Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘
- 输入的查询SQL语句,是如何执行的?
- Installation of postman and postman interceptor
- v-if VS v-show 2.0
- The architect started to write a HelloWorld
- Excuse me, my request is a condition update, but it is blocked in the buffer. In this case, can I only flush the cache every time?
- 汇编-入门
- 灵魂三问:什么是接口测试,接口测试怎么玩,接口自动化测试怎么玩?
猜你喜欢

程序员的视力怎么样? | 每日趣闻

postman和postman interceptor的安装

企业级:Spire.Office for .NET:Platinum|7.7.x

What is test development? Why do so many companies hire test developers now?

Enterprise level: spire Office for . NET:Platinum|7.7. x

Interview summary: This is a comprehensive & detailed Android interview guide

【软件逆向-基础知识】分析方法、汇编指令体系结构
![[software reverse - basic knowledge] analysis method, assembly instruction architecture](/img/97/8001db1c572495a115d32d9dd7360e.png)
[software reverse - basic knowledge] analysis method, assembly instruction architecture

How to use jedis of redis

JWT vulnerability recurrence
随机推荐
[array]566 Reshape the matrix - simple
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)
De debugging (set the main thread as hidden debugging to destroy the debugging Channel & debugger detection)
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
Clickhouse synchronization MySQL (based on materialization engine)
[C language] address book - dynamic and static implementation
Redis6-01nosql database
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
[an Xun cup 2019] not file upload
[system security] ten thousand words summary system virtualization container bottom layer principle experiment
On the day 25K joined Tencent, I cried
[wp]bmzclub writeup of several questions
Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
How to define a unified response object gracefully
[wp][入门]刷弱类型题目
MySQL winter vacation self-study 2022 11 (9)
[PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
Use object composition in preference to class inheritance
error Couldn‘t find a package.json file in “你的路径“
[web Audit - source code disclosure] obtain source code methods and use tools