当前位置:网站首页>(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;
}
边栏推荐
- 马斯克的「元宇宙」梦
- 20211018 some special matrices
- JUC atomic reference and ABA problem
- Neo4j environment construction
- C language: deep understanding of pointers and arrays
- 1-4 message passing interface [CSP authentication]
- A static variable is associated with a class and can be used as long as the class is in memory (the variable does not exist as long as your application terminates). (heap body, stack reference)
- C language: preprocessing in program environment
- Redis fuzzy query batch deletion
- Batch read all voice files under the folder
猜你喜欢

Exploitation of competitive loopholes in attacking and defending world PWN play conditions

an error occurred while trying to rename a file in the destination directory code 5

Longadder of the source code of JUC atomic accumulator

20220524 how to install coppeliasim to disk D

线上调试工具Arthas高级

C language: five custom types

20211020 academician all drive system

Jenkins accédant à l'authentification de l'utilisateur openldap

Heap

Final principle
随机推荐
1-2 24:00 (20 points) [CSP certification true question]
Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
C language: minesweeping
C language: deep understanding of character functions and string functions (1)
JUC atomic reference and ABA problem
批量读取文件夹下的全部语音文件
Jenkins集成Ldap,Ldap配置错误导致jenkins用户登录失败问题解决
静态变量与一个类相关联,只要该类在内存中(只要您的应用程序终止,该变量就不存在)就可以使用。(堆本体,栈引用)
C language: recursive function to realize Hanoi Tower
Leetcode points to offer 30 Stack containing min function
LeetCode 1143. 最长公共子序列
【最全面详细解释】背包问题详解
Musk's "meta universe" dream
Sort() sort function
[most comprehensive and detailed explanation] detailed explanation of knapsack problem
LeetCode 201. 数字范围按位与
Solov2 source code analysis
Delete soft link
C language: summary of question brushing (1)
Simple implementation of database link pool