Chef Shortest Path in Binary Trees
2014-04-05 09:35
246 查看
如果不在同一层,大的往上;在同一层则同时往上。直到根相同
#include<stdio.h>
void swap(int &x,int &y){
x=x^y;
y=x^y;
x=x^y;
}
int main(){
int tt,x,y,z;
scanf("%d",&tt);
while(tt--){
scanf("%d %d",&x,&y);
if(x>y) swap(x,y);
int ans=0;
while(x!=y){
if(y>=x*2){
y/=2;ans++;
}
else{
x/=2,y/=2;
ans+=2;
}
}
printf("%d\n",ans);
}
}
#include<stdio.h>
void swap(int &x,int &y){
x=x^y;
y=x^y;
x=x^y;
}
int main(){
int tt,x,y,z;
scanf("%d",&tt);
while(tt--){
scanf("%d %d",&x,&y);
if(x>y) swap(x,y);
int ans=0;
while(x!=y){
if(y>=x*2){
y/=2;ans++;
}
else{
x/=2,y/=2;
ans+=2;
}
}
printf("%d\n",ans);
}
}
相关文章推荐
- Summary: Lowest Common Ancestor in a Binary Tree & Shortest Path In a Binary Tree
- [转]Using Binary Heaps in A* Pathfinding
- HDU 4725 The Shortest Path in Nya Graph (最短路)
- hdu 4725 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph(最短路)
- hdu The Shortest Path in Nya Graph(建图+spfa)
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
- Sicily Shortest path in unweighted graph
- 使用selemium Remote调用firefox报"WebDriver: Cannot find firefox binary in PATH."的解决办法
- P - The Shortest Path in Nya Graph(拆点+dijkstra优先队列优化)
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
- hdu The Shortest Path in Nya Graph(建图+spfa)
- The Shortest Path in Nya Graph
- 本地运行hadoop-Failed to locate the winutils binary in the hadoop binary path
- Cannot find firefox binary in PATH的解决办法
- HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】
- HDU 1009 The Shortest Path in Nya Graph
- 2013成都邀请赛J题||HDU4725 The Shortest Path in Nya Graph(spfa+slf优化最短路)
- hdu The Shortest Path in Nya Graph(建图+spfa)
- Failed to locate the winutils binary in the hadoop binary path