题解 | #树上最短路#
树上最短路
http://www.nowcoder.com/questionTerminal/3ead0c7164344b7a873a5916bf05de33
不断除2找一样的根就行了
#include <bits/stdc++.h> using namespace std; int T, n, m, k, l, r; int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); int cnt = 0; while (n != m) { if (n > m) { n /= 2; cnt++; } else if (m > n){ m /= 2; cnt++; } } printf("%d\n", cnt); } return 0; }