a53

binary_tree

a53
Jan 24th, 2021 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int lca(int u,int v)
  5. {
  6. int sol=0;
  7. while(u!=v)
  8. {
  9. if(v<u)
  10. swap(u,v);
  11. v>>=1;
  12. ++sol;
  13. }
  14. return sol;
  15. }
  16.  
  17. int main()
  18. {
  19. ios_base::sync_with_stdio(false);
  20. cin.tie(nullptr);
  21. cout.tie(nullptr);
  22. int q,u,v;
  23. cin>>q;
  24. while(q--)
  25. cin>>u>>v,cout<<lca(u,v)<<'\n';
  26. return 0;
  27. }
Add Comment
Please, Sign In to add comment