求助,玄关
查看原帖
求助,玄关
678876
ac_lxy楼主2024/6/1 20:10
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
vector<int> g[N],f[N];
bool flag[N];
bool to[N];
bool vis[N];
int step[N];
void dfs(int u){
	flag[u]=1;
	for(auto v:f[u])if(!flag[v])dfs(v);
}
bool check(int u){
	for(auto v:g[u])if(!flag[v])return false;
	return true;
}
void bfs(int s,int t){
	queue<int> q;
	q.push(s);
	vis[s]=1;
	step[s]=0;
	while(q.size()){
		int u=q.front();
		q.pop();
		if(u==t){
			cout<<step[t]<<endl;
			return;
		}
		for(auto v:g[u]){
			if(!vis[v]&&to[v]){
				vis[v]=1;
				step[v]=step[u]+1;
				q.push(v);
			}
		}
	}
	cout<<-1<<endl;
}
int main(){
	int n,m,s,t;
	cin>>n>>m;
	while(m--){
		int x,y;
		cin>>x>>y;
		g[x].push_back(y);
		f[y].push_back(x);
	}
	cin>>s>>t;
	dfs(t);
	for(int i=1;i<=n;i++)to[i]=check(i);
	bfs(s,t);
	return 0;
}

为何错了

2024/6/1 20:10
加载中...