Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MAX = 21001;
  6.  
  7. int best;
  8. vector < int > myVector[MAX];
  9. bitset < MAX > beenThere;
  10. queue < int > myQueue;
  11. int D[MAX];
  12. int T,N,M;
  13.  
  14. inline void Read()
  15. {
  16.     scanf("%d%d", &N , &M);
  17.  
  18.     for ( int i = 1; i <= M ; ++i)
  19.     {
  20.         int x,y;
  21.         scanf("%d%d", &x , &y);
  22.  
  23.         myVector[x].push_back(y);
  24.         myVector[y].push_back(x);
  25.     }
  26. }
  27.  
  28. inline int BFS(int currentNode)
  29. {
  30.     myQueue.push(currentNode);
  31.     beenThere[currentNode] = true;
  32.     best = 0;
  33.  
  34.     while(!myQueue.empty())
  35.     {
  36.         int node = myQueue.front();
  37.         myQueue.pop();
  38.  
  39.         for ( auto &neighbor : myVector[node])
  40.         {
  41.             //int neighbor = myVector[node][k];
  42.             if(!beenThere[neighbor])
  43.             {
  44.                 myQueue.push(neighbor);
  45.                 beenThere[neighbor] = true;
  46.                 D[neighbor] = D[node]+1;
  47.                 best = D[neighbor];
  48.  
  49.             }
  50.         }
  51.     }
  52.     return best;
  53. }
  54.  
  55. int main()
  56. {
  57.     freopen("amici2.in" , "r" , stdin);
  58.    freopen("amici2.out" , "w" ,stdout);
  59.  
  60.      scanf("%d", &T);
  61.  
  62.     for ( int i = 1 ; i <= T ; ++i)
  63.     {
  64.        for ( int j = 1; j <= N ; ++j)
  65.             myVector[j].clear();
  66.  
  67.             beenThere.reset();
  68.             Read();
  69.         int Answer = ceil(log2(BFS(1)));
  70.  
  71.  
  72.         printf("%d\n" , Answer);
  73.  
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement