fork download
  1. // #include <bits/stdc++.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <map>
  6. #include <set>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <math.h>
  10. #include <iomanip>
  11. #include <iterator>
  12.  
  13. using namespace std;
  14.  
  15. #define PB push_back
  16. #define MP make_pair
  17. #define f first
  18. #define s second
  19. #define FOR(i,a,b) for (int i = a; i < b; ++i)
  20. #define RFOR(i, b, a) for (int i = b-1; i>=a; --i)
  21. #define FILL(a, b) memset(a, b, sizeof a)
  22. #define ALL(a) a.begin(), a.end()
  23. #define SZ(a) (int)a.size()
  24.  
  25. typedef long long LL;
  26. typedef pair<double, double> PII;
  27. typedef vector<int> VI;
  28. typedef vector<LL> VLL;
  29. typedef pair<LL, LL> PLL;
  30.  
  31. const int INF = 1000 * 1000 * 1000;
  32. const LL LINF = 1LL * INF * INF;
  33. const long double PI = acos(-1.);
  34. const double EPS = 1e-6;
  35. LL mod = INF + 7;
  36.  
  37. const int maxn = 3*100010;
  38. VI g[maxn];
  39. int d[maxn];
  40. int q[maxn];
  41. int main()
  42. {
  43. // freopen("input.txt","r",stdin);
  44. int n,m;
  45. cin >> n >> m;
  46. FOR(i,0,m)
  47. {
  48. int a,b;
  49. cin >> a >> b;
  50. a--;b--;
  51. g[3*a].PB(3*b+1);
  52. g[3*a+1].PB(3*b+2);
  53. g[3*a+2].PB(3*b);
  54. }
  55. int s,t;
  56. cin >> s >> t;
  57. s--; t--;
  58. FOR(i,0,maxn)
  59. d[i] = INF;
  60. d[3*s] = 0;
  61. int qh = 0,qt = 0;
  62. q[qh++] = 3*s;
  63. while(qh!=qt)
  64. {
  65. int v = q[qt++];
  66. FOR(i,0,SZ(g[v]))
  67. {
  68. if(d[g[v][i]]!=INF) continue;
  69. q[qh++] = g[v][i];
  70. d[g[v][i]] = d[v] + 1;
  71. }
  72. }
  73.  
  74.  
  75. if(d[3*t]==INF)
  76. {
  77. cout << -1;
  78. }
  79. else
  80. {
  81. cout << d[3*t]/3;
  82. }
  83.  
  84. return 0;
  85. }
Runtime error #stdin #stdout 0s 29760KB
stdin
Standard input is empty
stdout
Standard output is empty