题目:http://noi.openjudge.cn/ch0306/1758/
分析:因为节点序号有序,所以此题有若干做法.我采用了父节点是“序号/2”的做法.
代码:
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int x,y,dx,dy;
int main()
{
scanf("%d%d",&x,&y);
if(x<y) swap(x,y);
dx=log(x)/log(2);dy=log(y)/log(2);
while(dx>dy)
{
x/=2;
dx=log(x)/log(2);
}
while(x!=y)
{
x/=2;
y/=2;
}
printf("%d",x);
return 0;
}