Language: C++

#include <bits/stdc++.h> 
using namespace std;

typedef long long ll;

#define rep(a,b,c) for(int a=b,a##end=c;a<=a##end;++a)
#define drep(a,b,c) for(int a=b,a##end=c;a>=a##end;--a)


char IO;
int rd(){
	int s=0,f=0;
	while(IO=getchar(),IO<'0'||IO>'9')if(IO=='-')f=1;
	do s=(s<<1)+(s<<3)+(IO^'0');
	while(IO=getchar(),IO>='0'&&IO<='9');
	return f?-s:s;
}

const int N=1e5+10,P=1e9+7;

int n,m;

int dis[N][3];



struct Edge{
	int to,nxt;
}e[N];
int head[N],ecnt;
void AddEdge(int u,int v){
	e[++ecnt]=(Edge){v,head[u]};
	head[u]=ecnt;
}

int inq[N][3];
queue<pair<int,int> >Q;
void SPFA(int st){
	Q.push((pair<int,int>){st,0});dis[st][0]=0;
	while(!Q.empty()){
		int u=Q.front().first,k=Q.front().second;
		Q.pop();
		int nk=(k+1)%3;
		inq[u][k]=0;
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(dis[v][nk]>dis[u][k]+1){
				dis[v][nk]=dis[u][k]+1;
				if(!inq[v][nk]) Q.push((pair<int,int>){v,nk}),inq[v][nk]=1;
			}
		}
	}
}


int main(){
	memset(dis,127,sizeof dis);
	n=rd(),m=rd();
	rep(i,1,m){
		int u=rd(),v=rd();
		AddEdge(u,v);
	}
	int s=rd(),t=rd();
	SPFA(s);
	int ans=dis[t][0];
	if(ans<1e9) printf("%d\n",ans/3);
	else puts("-1");
}






#include <bits/stdc++.h> 
using namespace std;

typedef long long ll;

#define rep(a,b,c) for(int a=b,a##end=c;a<=a##end;++a)
#define drep(a,b,c) for(int a=b,a##end=c;a>=a##end;--a)


char IO;
int rd(){
	int s=0,f=0;
	while(IO=getchar(),IO<'0'||IO>'9')if(IO=='-')f=1;
	do s=(s<<1)+(s<<3)+(IO^'0');
	while(IO=getchar(),IO>='0'&&IO<='9');
	return f?-s:s;
}

const int N=1e5+10,P=1e9+7;

int n,m;

int dis[N][3];



struct Edge{
	int to,nxt;
}e[N];
int head[N],ecnt;
void AddEdge(int u,int v){
	e[++ecnt]=(Edge){v,head[u]};
	head[u]=ecnt;
}

int inq[N][3];
queue<pair<int,int> >Q;
void SPFA(int st){
	Q.push((pair<int,int>){st,0});dis[st][0]=0;
	while(!Q.empty()){
		int u=Q.front().first,k=Q.front().second;
		Q.pop();
		int nk=(k+1)%3;
		inq[u][k]=0;
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(dis[v][nk]>dis[u][k]+1){
				dis[v][nk]=dis[u][k]+1;
				if(!inq[v][nk]) Q.push((pair<int,int>){v,nk}),inq[v][nk]=1;
			}
		}
	}
}


int main(){
	memset(dis,127,sizeof dis);
	n=rd(),m=rd();
	rep(i,1,m){
		int u=rd(),v=rd();
		AddEdge(u,v);
	}
	int s=rd(),t=rd();
	SPFA(s);
	int ans=dis[t][0];
	if(ans<1e9) printf("%d\n",ans/3);
	else puts("-1");
}








Compiler output:
a.cc:84:6: error: redefinition of 'char IO'
   84 | char IO;
      |      ^~
a.cc:10:6: note: 'char IO' previously declared here
   10 | char IO;
      |      ^~
a.cc:85:5: error: redefinition of 'int rd()'
   85 | int rd(){
      |     ^~
a.cc:11:5: note: 'int rd()' previously defined here
   11 | int rd(){
      |     ^~
a.cc:93:11: error: redefinition of 'const int N'
   93 | const int N=1e5+10,P=1e9+7;
      |           ^
a.cc:19:11: note: 'const int N' previously defined here
   19 | const int N=1e5+10,P=1e9+7;
      |           ^
a.cc:93:20: error: redefinition of 'const int P'
   93 | const int N=1e5+10,P=1e9+7;
      |                    ^
a.cc:19:20: note: 'const int P' previously defined here
   19 | const int N=1e5+10,P=1e9+7;
      |                    ^
a.cc:95:5: error: redefinition of 'int n'
   95 | int n,m;
      |     ^
a.cc:21:5: note: 'int n' previously declared here
   21 | int n,m;
      |     ^
a.cc:95:7: error: redefinition of 'int m'
   95 | int n,m;
      |       ^
a.cc:21:7: note: 'int m' previously declared here
   21 | int n,m;
      |       ^
a.cc:97:5: error: redefinition of 'int dis [100010][3]'
   97 | int dis[N][3];
      |     ^~~
a.cc:23:5: note: 'int dis [100010][3]' previously declared here
   23 | int dis[N][3];
      |     ^~~
a.cc:101:8: error: redefinition of 'struct Edge'
  101 | struct Edge{
      |        ^~~~
a.cc:27:8: note: previous definition of 'struct Edge'
   27 | struct Edge{
      |        ^~~~
a.cc:103:2: error: conflicting declaration 'int e [100010]'
  103 | }e[N];
      |  ^
a.cc:29:2: note: previous declaration as 'Edge e [100010]'
   29 | }e[N];
      |  ^
a.cc:104:5: error: redefinition of 'int head [100010]'
  104 | int head[N],ecnt;
      |     ^~~~
a.cc:30:5: note: 'int head [100010]' previously declared here
   30 | int head[N],ecnt;
      |     ^~~~
a.cc:104:13: error: redefinition of 'int ecnt'
  104 | int head[N],ecnt;
      |             ^~~~
a.cc:30:13: note: 'int ecnt' previously declared here
   30 | int head[N],ecnt;
      |             ^~~~
a.cc:105:6: error: redefinition of 'void AddEdge(int, int)'
  105 | void AddEdge(int u,int v){
      |      ^~~~~~~
a.cc:31:6: note: 'void AddEdge(int, int)' previously defined here
   31 | void AddEdge(int u,int v){
      |      ^~~~~~~
a.cc:110:5: error: redefinition of 'int inq [100010][3]'
  110 | int inq[N][3];
      |     ^~~
a.cc:36:5: note: 'int inq [100010][3]' previously declared here
   36 | int inq[N][3];
      |     ^~~
a.cc:111:22: error: redefinition of 'std::queue<std::pair<int, int> > Q'
  111 | queue<pair<int,int> >Q;
      |                      ^
a.cc:37:22: note: 'std::queue<std::pair<int, int> > Q' previously declared here
   37 | queue<pair<int,int> >Q;
      |                      ^
a.cc:112:6: error: redefinition of 'void SPFA(int)'
  112 | void SPFA(int st){
      |      ^~~~
a.cc:38:6: note: 'void SPFA(int)' previously defined here
   38 | void SPFA(int st){
      |      ^~~~
a.cc:130:5: error: redefinition of 'int main()'
  130 | int main(){
      |     ^~~~
a.cc:56:5: note: 'int main()' previously defined here
   56 | int main(){
      |     ^~~~