本蒟蒻整了接近三小时,交了n遍,9个点都RE,好不容易整出来了(原来是掉了个边界)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,x,y,s,t,num,sum,f2;
int h[10050],fla[10050],fl[10050];
int q[5000000],zd[10050];
struct edge{
int next1;
int to1;
}a[5000000];
void bfs1(int x)
{
int hhh=1,ttt=1;
q[hhh]=x; fla[x]=1;
while(hhh<=ttt)
{
x=q[hhh];
for(int i=h[x];i>0;i=a[i].next1)
if(fla[a[i].to1]==0)
{
ttt++;
q[ttt]=a[i].to1;
fla[a[i].to1]=1;
}
hhh++;
}
}
void bfs2(int x,int y)
{
memset(q,0,sizeof(q));
int hd=1,tl=1;
q[hd]=x;
while(hd<=tl)
{
int x1=q[hd];
for(int i=h[x1];i>0;i=a[i].next1)
{
if(fla[a[i].to1]==1)//边界问题,哎
{
tl++;
q[tl]=a[i].to1;
zd[tl]=zd[hd]+1;
if(q[tl]==y){cout<<zd[tl]; f2=1;}
fla[q[tl]]=0;
}
}
hd++;
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>x>>y;
a[i].next1=h[y];
a[i].to1=x;
h[y]=i;
}
cin>>s>>t;
bfs1(t);
for(int i=1;i<=n;i++)
if(fla[i]==0)
for(int j=h[i];j>0;j=a[j].next1)
fl[a[j].to1]=1;
for(int i=1;i<=n;i++)
if(fla[i]!=0&&fl[i]==1)
fla[i]=0;
bfs2(t,s);
if(f2==0) cout<<"-1";
return 0;
}