題目鏈接 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this ...
Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people. Input First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated by a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you are to answer the distance between house i and house j. Output For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case. Sample Input 2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1 Sample Output 10 25 100 100
題意:有一棵有n個節點的樹,每條邊上有一個權值代表這兩個點之間的距離,現在m次詢問:從節點a到節點b的路徑長?
思路:預處理所有節點到根節點(定為節點1)的距離,以及所有節點的祖先信息(fa[i][j]表示節點 i 向上距離為 (1<<j)的祖先節點編號),計算a和b到根節點的距離和,減去兩倍的最近公共祖先的到根節點的距離值。
代碼如下:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; const int N = 4e4 + 5; int head[N], cnt; struct Edge { int to, next; int value; }e[2 * N]; struct Node { int fa[20]; int deep; int sum; bool state; }node[N]; void insert(int u, int v, int value) { e[++cnt].to = v; e[cnt].next = head[u]; e[cnt].value = value; head[u] = cnt; e[++cnt].to = u; e[cnt].next = head[v]; e[cnt].value = value; head[v] = cnt; } int cal(int x, int t) { for (int i = 0; i <= 19; i++) if (t&(1 << i)) x = node[x].fa[i]; return x; } void dfs(int x) { node[x].state = 1; for (int i = 1; i <= 19; i++) { if (node[x].deep<(1 << i))break; node[x].fa[i] = node[node[x].fa[i - 1]].fa[i-1];///倍增處理祖先信息 } for (int i = head[x]; i; i = e[i].next) { if (node[e[i].to].state) continue; node[e[i].to].deep = node[x].deep+ 1; node[e[i].to].fa[0] = x; node[e[i].to].sum = node[x].sum+e[i].value; dfs(e[i].to); } } int lca(int x, int y)///求lca { if (node[x].deep<node[y].deep) swap(x, y); x = cal(x, node[x].deep - node[y].deep); for (int i = 19; i >= 0; i--) if (node[x].fa[i] != node[y].fa[i]) { x = node[x].fa[i]; y = node[y].fa[i]; } if (x == y)return x; else return node[x].fa[0]; } void init() { cnt = 0; memset(head, 0, sizeof(head)); memset(node, 0, sizeof(node)); } int main() { int T; cin >> T; while (T--) { init(); int n, m; cin >> n >> m; for (int i = 0; i < n-1; i++) { int x, y, v; scanf("%d%d%d",&x,&y,&v); insert(x,y,v); } dfs(1); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d",&x,&y); int pa = lca(x, y); int ans = node[x].sum - node[pa].sum + node[y].sum - node[pa].sum; cout << ans << endl; } } return 0; }