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): 2090Problem DescriptionThere 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.
InputFirst 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.
OutputFor each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
Sample Output10 25 100 100
Accepted Code:
1 /*************************************************************************
2 > File Name: 2586.cpp
3 > Author: Stomach_ache
4 > Mail: sudaweitong@gmail.com
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 }
posted on 2014-09-04 10:31 Stomach_ache 阅读(168) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步