Codechef April Challenge 2014, Long Contest BINTREE - "Shortest Path in Binary Trees" editorial: http://discuss.codechef.com/questions/41181/bintree-editorial
Codechef April Challenge 2014, Long Contest BINTREE - "Shortest Path in Binary Trees" solution:
#include <iostream> #include <cstdio> using namespace std; int main() { int n, same, i_st, j_st, c; long long int i, j, a[35], b[35], depth[35]; depth[0]=1; for(int m=1; m<33; m++) depth[m]=depth[m-1]<<1; scanf("%d", &n); while(n--) { for(int m=0; m<35; m++) a[m]=b[m]=0; i_st=j_st=same=0; scanf("%lld%lld", &i, &j); c=0; while(i>=depth[c]) c++; for(int m=c-1; m>=0; m--) a[m]=i, i=i>>1; i_st=c; c=0; while(j>=depth[c]) c++; for(int m=c-1; m>=0; m--) b[m]=j, j=j>>1; j_st=c; for(int m=0; m<33; m++) if(a[m]!=b[m] || a[m]==0 || b[m]==0) { same=m; break; } printf("%d\n", i_st+j_st-2*same); } return 0; }
No comments:
Post a Comment