program road; const maxn=10450; type node=^ha; ha=record v:longint; next:node; end; var d,q:array[0..maxn]of longint; f,ff:array[0..maxn]of node; g,gg:array[0..maxn]of boolean; i,j,k,n,m,x,y,s,t:longint; head,tail:longint; p:node; procedure dfs; var i:longint; begin fillchar(g,sizeof(g),false); fillchar(gg,sizeof(gg),false); fillchar(q,sizeof(q),0); head:=0;tail:=1; q[1]:=t; g[t]:=true;gg[t]:=true; repeat inc(head);if head>maxn then head:=1; p:=ff[q[head]]; while p<>nil do begin i:=p^.v; if not g[i] then begin g[i]:=true; gg[i]:=true; inc(tail);if tail>maxn then tail:=1; q[tail]:=i; end; p:=p^.next; end; until head>tail; for i:=1 to n do if not gg[i] then begin p:=ff[i]; while p<>nil do begin g[p^.v]:=false; p:=p^.next; end; end; end; procedure spfa; var i:longint; begin fillchar(q,sizeof(q),0); fillchar(d,sizeof(d),$7f); head:=0;tail:=1;q[1]:=s; d[s]:=0; repeat inc(head);if head>maxn then head:=1; i:=q[head]; p:=f[i]; while p<>nil do begin if (g[p^.v])and(d[p^.v]>d[i]+1)then begin d[p^.v]:=d[i]+1; inc(tail);if tail>maxn then tail:=1; q[tail]:=p^.v; end; p:=p^.next; end; until head>tail; if d[t]=d[maxn] then writeln(-1)else writeln(d[t]); end; begin readln(n,m); for i:=1 to n do begin f[i]:=nil;ff[i]:=nil;end; for i:=1 to m do begin readln(x,y); if x<>y then begin new(p); p^.v:=y;p^.next:=f[x];f[x]:=p; new(p); p^.v:=x;p^.next:=ff[y];ff[y]:=p; end; end; readln(s,t); dfs; spfa; end. /************************************************************** Problem: 2338 User: admin Language: Pascal Result: Wrong Answer ****************************************************************/