fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define el cout << '\n'
  5. #define ii pair<int, int>
  6. #define fi first
  7. #define se second
  8.  
  9. using namespace std;
  10.  
  11. const int maxn = 255;
  12. const int INF = 1e9;
  13.  
  14. int n, m, d[maxn + 10][maxn + 10], s, t;
  15. vector<int> adj[maxn + 10];
  16. bool vis[maxn + 10][maxn + 10];
  17.  
  18. int main()
  19. {
  20. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  21. if (fopen("BAGENTS.INP", "r"))
  22. {
  23. freopen("BAGENTS.INP", "r", stdin);
  24. freopen("BAGENTS.OUT", "w", stdout);
  25. }
  26.  
  27. cin >> n >> m;
  28. cin >> s >> t;
  29. for (int i = 1; i <= m; i++)
  30. {
  31. int x, y;
  32. cin >> x >> y;
  33. adj[x].push_back(y);
  34. }
  35. d[s][t] = 0;
  36. vis[s][t] = 1;
  37. queue<ii> q;
  38. q.push({s, t});
  39.  
  40. while (!q.empty())
  41. {
  42. ii t = q.front();
  43. q.pop();
  44. int x = t.fi;
  45. int y = t.se;
  46.  
  47. if (x == y) return cout << d[x][y], 0;
  48.  
  49. for (int next_x : adj[x])
  50. for (int next_y : adj[y])
  51. {
  52. if (vis[next_x][next_y]) continue;
  53. d[next_x][next_y] = d[x][y] + 1;
  54. vis[next_x][next_y] = 1;
  55. q.push({next_x, next_y});
  56. }
  57. }
  58. cout << -1;
  59. }
  60.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty