当前位置:网站首页>(bfs) acwing 847. Hierarchy of points in the graph
(bfs) acwing 847. Hierarchy of points in the graph
2022-06-13 09:24:00 【Age worry】
847. The hierarchy of dots in the graph
Topic link https://www.acwing.com/problem/content/849/
subject :
Ideas : The number of points like this is very large , You can think about it bfs Can we solve , because bfs The complexity of is the number of edges
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e5+10;
int h[N],e[N],ne[N],idx;
int dis[N];
int n,m;
void add(int x,int y){
e[idx]=y,ne[idx]=h[x],h[x]=idx++;
}
void bfs(){
dis[1]=0;
queue<int > q;
q.push(1);
while(q.size()){
int u=q.front();
q.pop();
for(int i=h[u];i!=-1;i=ne[i]){
int t=e[i];
if(!dis[t]){
dis[t]=dis[u]+1;
q.push(t);
}
}
}
}
int main(){
cin>>n>>m;
int x,y;
memset(h,-1,sizeof h);
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
add(x,y);
}
bfs();
if(n==1){
cout<<0;
}else{
if(dis[n]==0){
cout<<-1;
}else{
cout<<dis[n];
}
}
return 0;
}
边栏推荐
- LeetCode 72. 编辑距离
- LeetCode 6097. 替换字符后匹配(字典)
- 20211006 integral, differential and projection belong to linear transformation
- LeetCode 6098. Count the number of subarrays whose score is less than K (prefix and + binary search)
- LeetCode 6095. 强密码检验器 II
- Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
- 虚拟化和云计算文章大合集
- an error occurred while trying to rename a file in the destination directory code 5
- C language 7-13 day K candle chart (15 points)
- Lecture par lots de tous les fichiers vocaux sous le dossier
猜你喜欢
C language: dynamic memory management
Solov2 source code analysis
攻防世界PWN play 条件竞争漏洞的利用
Class loading overview
SQL ROW_ The number() function uses
C language: deep understanding of pointers and arrays
C language: file operation
JUC 原子累加器 源码之 LongAdder
Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
20220606 Young's inequality for Matrices
随机推荐
LeetCode 6097. Match after replacing characters (Dictionary)
Overview of common layers of image recognition neural network (under update)
I set up a blog
Z字形变换
LeetCode 202. Happy number
20211005 Hermite matrix and some properties
Sort() sort function
Figure introduction to database neo4j
虚拟化和云计算文章大合集
C language: recursive function to realize Hanoi Tower
Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
C language: file operation
Exploitation of competitive loopholes in attacking and defending world PWN play conditions
LeetCode 5259. Calculate the total tax payable
Routing - static routing
The turtle library displays the system time
Heap
JUC atomic accumulator
C language: deep understanding of character functions and string functions (2)
JUC原子整数