当前位置:网站首页>Explanation of the taste of snow vegetables -- searching for Literature
Explanation of the taste of snow vegetables -- searching for Literature
2022-07-18 23:59:00 【LiSymbol】
【 Deep base 18. example 3】 Search for Literature - Luogu
label :bfs、dfs、 graph theory 、 The template questions
Ideas : Grand classic graph dfs and bfs, But here is a special case , It requires rules from small to large to search , So we need to sort first , Preprocess something , See code for details
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
const int N=1000010;
int h[N],e[N],ne[N],idx;
bool st[N]; // Weight array , Judge whether the current node has been traversed , Prevent repeated traversal
struct node{
// Defining structure , Used to give the order of connection of edges , Sort
int x,y;
}arr[N];
void add(int a,int b){
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
bool cmp(node a,node b){
// Prioritize Small node
if(a.x!=b.x){
// Then how can a point have multiple edges , We should put the big ones in front , Why? ?
return a.x<b.x; // because ,add() It's head insertion , So in reverse order , You can draw a picture to understand
}else{
return a.y>b.y;
}
}
void dfs_map(int u){
st[u]=true;
cout<<u<<' ';
for(int i=h[u];i!=-1;i=ne[i]){
int j=e[i];
if(!st[j]){
dfs_map(j);
}
}
}
// Next is the general graph bfs and dfs The board is broken
void bfs_map(){
memset(st,0,sizeof st);
queue<int> que;
st[1]=true;
que.push(1);
while(!que.empty()){
auto t=que.front();
que.pop();
cout<<t<<' ';
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
if(!st[j]){
st[j]=true;
que.push(j);
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
memset(h,-1,sizeof h);
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
arr[i].x=a;
arr[i].y=b;
}
sort(arr,arr+m,cmp);
for(int i=0;i<m;i++){
// cout<<arr[i].x<<"--"<<arr[i].y<<endl;
add(arr[i].x,arr[i].y);
}
dfs_map(1);
cout<<endl;
bfs_map();
}
@ I'm not very good at expressing , The algorithm is just a glimpse of the path , If you don't understand anything , Where the writing is not good , Feel free to leave a comment in the comments section , I'll learn a lesson , Improve yourself slowly , thank you !
边栏推荐
- How to use redis to realize distributed cache
- fiddler抓不到PC端微信小程序的包
- AtCoder Beginner Contest 259 D Circumferences
- VMware6.0连接群晖iscsi
- E. Split Into Two Sets
- Glide 源码分析(4.13.2)
- Programming examples of stm32f1 and stm32cube ide-w25q-spi-flash and SPIFs porting
- 乐观锁和悲观锁在kubernetes中的应用
- OpenCV DFT
- WordPress主题分享:Flatsome主题v3.15.7免费下载 2022年最新版
猜你喜欢

WordPress主题分享:The7主题v10.11免费下载 2022年最新版

E. Split Into Two Sets

Shh! Fishing artifact, don't let the boss know| Real time voice to text, fast time sequence prediction, yolov6 can be used in one line of command to sort out CSV | showmeai Information Daily

WordPress Theme sharing: the7 theme v10.11 download the latest version of 2022 for free

最大子数组异或和

Redis分布式缓存-数据持久化

7. Common garbage collectors

canvas无数个三角形动画js特效

Introduction to the universal theme system of SAP appgyver

Applet: the picker view selector scrolls quickly. When confirming, the "value displays an error."“
随机推荐
P5.js grow up JS special effect
WordPress Theme sharing: flatsome theme v3.15.7 download the latest version in 2022 for free
Use of res.cc
Openpose: real time multiplayer 2D pose estimation using partial affinity fields
9. Learn to check GC logs
【百度飞桨】手写数字识别模型部署Paddle Inference
MySQL安裝常見報錯怎麼處理
WordPress主题分享:Avada主题v7.8.0免费下载 2022年最新版
Une seule ligne de texte dépasse l'ellipse partielle, plusieurs lignes de texte dépassent l'ellipse partielle, spécifiez plusieurs lignes
找到字符串中所有字母异位词
Introduction to the universal theme system of SAP appgyver
5. < tag dynamic programming and complete knapsack problem> lt.70 Climb stairs | | advanced version + lt.322 Change + lt.139 Word split + lt.279 Complete square DBC
liunx中FileDescriptor与打开文件之间的关系
nodeJS中利用第三方内置模块实现数字转大写功能
Summary of binary search questions
MySQL安装常见报错怎么处理
In depth explanation of MySQL index
Buffer pool production practice
nodeJS中使用promise实现文件读取、写入的案例
Pytest interface automation test framework | @pytest Fixture () decorator