Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector< int > edge[800005];
  6. bool vis[800005];
  7. int lvl[800005];
  8.  
  9. void bfs( int src )
  10. {
  11.     queue < int > q;
  12.     memset( vis, 0, sizeof vis );
  13.     memset( lvl, 0, sizeof lvl );
  14.  
  15.     q.push( src );
  16.     vis[ src ] = true;
  17.     while( !q.empty() ){
  18.         int u = q.front();
  19.         q.pop();
  20.  
  21.         for( int i = 0; i<edge[u].size(); i++){
  22.             if( !vis[edge[u][i]] ){
  23.                 int v =edge[u][i];
  24.                 vis[v] = true;
  25.                 lvl = lvl[u]+1;
  26.                 q.push( v );
  27.             }
  28.         }
  29.     }
  30. }
  31.  
  32. int main() {
  33.     int T, n, x, y, m, s, e;
  34.     cin >> T;
  35.     while( T-- && cin >> n >> m ) {
  36.         for(int i=0; i<=m; i++) edge[i].clear();
  37.         int ara[m+5];
  38.         for( int i=1; i<=n; i++){
  39.             cin >> ara[i];
  40.         }
  41.         while( n-- ) {
  42.             cin >> x >> y;
  43.             edge[ x ].push_back( y );
  44.             edge[ y ].push_back( x );
  45.         }
  46.         cin >> s >> e;
  47.         bfs( s );
  48.         cout << lvl[e] << endl;
  49.  
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement