sabertooth09

Untitled

Jul 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include<iostream>
  2. #include<bits/stdc++.h>
  3. typedef long long lli;
  4. using namespace std;
  5. #include<algorithm>
  6. #include<vector>
  7. #include<queue>
  8.  
  9. #include<math.h>
  10. queue<lli>q;
  11. using  namespace std;
  12. lli distance1[10000];
  13. vector<lli>v[10000];
  14. lli bfs(lli start,lli end1)
  15. {
  16.     memset(distance1,0,sizeof(distance1));
  17.    lli visited[10000]={0};
  18.    q.push(start);
  19.    visited[start]=1;
  20.    while(!q.empty())
  21.    {
  22.        lli u=q.front();
  23.        q.pop();
  24.        for(lli i=0;i<v[u].size();i++)
  25.  
  26.        {
  27.            lli s=v[u][i];
  28.  
  29.            if(visited[s]==0)
  30.            {
  31.  
  32.  
  33.                visited[s]=1;
  34.                distance1[s]=distance1[u]+1;
  35.                q.push(s);
  36.  
  37.  
  38.  
  39.            }
  40.        }
  41.    }
  42. }
  43. int main()
  44. {
  45.     lli a;
  46.     cin>>a;
  47.     while(a--)
  48.     {
  49.         lli p,q;
  50.         cin>>p>>q;
  51.         v[p].push_back(q);
  52.         v[q].push_back(p);
  53.     }
  54.     lli s,e;
  55.     while(cin>>s>>e)
  56.     {
  57.         bfs(s,e);
  58.         cout<<distance1[e]<<endl;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment