Hdu 2586(LCA)

题目链接

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5491    Accepted Submission(s): 2090


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 bu 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 areato 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
Accepted Code:
 1 /*************************************************************************  2  > File Name: 2586.cpp  3  > Author: Stomach_ache  4  > Mail: [email protected]  5  > Created Time: 2014年09月03日 星期三 09时53分59秒  6  > Propose:  7  ************************************************************************/

 8 #pragma comment(linker, "/STACK:1024000000, 1024000000")

 9 #include <cmath>

10 #include <string>

11 #include <cstdio>

12 #include <vector>

13 #include <fstream>

14 #include <cstring>

15 #include <iostream>

16 #include <algorithm>

17 using namespace std; 18 /*Let's fight!!!*/

19 

20 typedef pair<int, int> pii; 21 const int MAX_V = 40050; 22 const int MAX_LOG_V = 20; 23 vector<pii> G[MAX_V]; 24 int root; 25 

26 int depth[MAX_V]; 27 int fa[MAX_LOG_V][MAX_V]; 28 int dis[MAX_V]; 29 

30 void dfs(int u, int p, int d) { 31       fa[0][u] = p; 32       depth[u] = d; 33     for (int i = 0; i < G[u].size(); i++) { 34           int v = G[u][i].first, w = G[u][i].second; 35           if (v != p) { 36             dis[v] = dis[u] + w; 37               dfs(v, u, d + 1); 38  } 39  } 40 } 41 

42 void init(int V) { 43       dis[1] = 0; 44     root = 1; 45       dfs(root, -1, 0); 46 

47     for (int k = 0; k + 1 < MAX_LOG_V; k++) { 48           for (int v = 1; v <= V; v++) { 49               if (fa[k][v] < 0) fa[k + 1][v] = -1; 50             else fa[k + 1][v] = fa[k][fa[k][v]]; 51  } 52  } 53 } 54 

55 int lca(int u, int v) { 56       if (depth[u] > depth[v]) swap(u, v); 57     for (int k = 0; k < MAX_LOG_V; k++) { 58           if ((depth[v] - depth[u]) >> k & 1) 59               v = fa[k][v]; 60  } 61 

62     if (u == v) return u; 63     for (int k = MAX_LOG_V - 1; k >= 0; k--) { 64           if (fa[k][u] != fa[k][v]) { 65               u = fa[k][u]; 66             v = fa[k][v]; 67  } 68  } 69     return fa[0][u]; 70 } 71 

72 int main(void) { 73       ios_base::sync_with_stdio(false); 74       int test; 75     cin >> test; 76     while (test--) { 77           int n, m; 78         cin >> n >> m; 79         for (int i = 1; i <= n; i++) G[i].clear(); 80 

81         for (int i = 1; i < n; i++) { 82               int u, v, w; 83             cin >> u >> v >> w; 84  G[u].push_back(pii(v, w)); 85  G[v].push_back(pii(u, w)); 86  } 87 

88  init(n); 89         while (m--) { 90               int u, v; 91             cin >> u >> v; 92             cout << dis[u] + dis[v] - dis[lca(u, v)] * 2 << endl; 93  } 94  } 95 

96     return 0; 97 }

 

你可能感兴趣的:(HDU)