求指点
查看原帖
求指点
1359141
mo_mo_yu0_0楼主2024/10/20 22:37
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,a,b,ans=1145141919;
bool vis[200010];
vector<ll>h[200010];
void dfs(ll d,ll k){
	if(d==1&&k){
		ans=min(k,ans);
	}
	if(d-1)vis[d]=1;
	for(int i:h[d]){
		if(!vis[i])dfs(i,k+1);
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>a>>b;
		h[a].push_back(b);
	}
	dfs(1,0);
	if(ans==1145141919)cout<<-1;
	else cout<<ans;
}

dfs

2024/10/20 22:37
加载中...