fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cmath>
  4. #include <string>
  5. #include <vector>
  6. #include <stack>
  7. #include <queue>
  8. #include <cassert>
  9. #include <ctime>
  10. #include <cstdlib>
  11. #include <algorithm>
  12. #define Pi 3.14159
  13. #define vi vector<int>
  14. #define pi pair<int,int>
  15. #define si stack<int>
  16.  
  17. typedef long long int ll;
  18. using namespace std;
  19.  
  20. bool b[3501][3501]={0};
  21.  
  22. int main ()
  23. {
  24. int n,m;
  25. cin >>n>>m;
  26. int u,v;
  27. for (int i =1;i<= m;i++)
  28. {
  29. scanf("%d",&u);
  30. scanf("%d",&v);
  31. b[u][v]=b[v][u]=1;
  32. }
  33. // input completed.
  34. int dist[n+1];
  35.  
  36. int h,V;
  37. cin >>h>>V;
  38. dist[h]=0;
  39. //cout<<"hero "<<h<<" "<<V<<endl;
  40. queue<int> q;
  41. bool bfs[3501];
  42. for (int i=1;i<= n;i++)bfs[i]=1;
  43. q.push(h);
  44. bfs[h]=0;
  45. while (!q.empty())
  46. {
  47. int top = q.front();
  48. // cout<<top<<endl;
  49. q.pop();
  50. for (int i = 1 ;i <= 3500;i++)
  51. {
  52. if(bfs[i] && b[i][top])
  53. {
  54. int x = i;
  55. dist[i] = dist[top] +1;
  56. if(x == V){cout<<dist[x]<<endl;return 0;}
  57. bfs[x]=0;
  58. q.push(x);
  59. }
  60. }
  61. }
  62. cout<<0<<endl;
  63. }
  64.  
  65.  
Success #stdin #stdout 0s 15448KB
stdin
5 3
1 3
1 2
4 5
1 4
stdout
0