题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2586

How far away ?

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

tarjan离线求LCA。。

#include <bits/stdc++.h>
using namespace std;
const int N = 40010;
struct Tarjan_Lca {
bool vis[N];
vector<pair<int, int>> A, Q[N];
int tot, par[N], ans[210], head[N], dist[N];
struct edge { int to, w, next; }G[N << 1];
inline void init(int n) {
A.clear();
for(int i = 0; i < n + 2; i++) {
par[i] = i;
vis[i] = false;
head[i] = -1;
dist[i] = 0;
Q[i].clear();
}
}
inline void add_edge(int u, int v, int w) {
G[tot] = { v, w, head[u] }, head[u] = tot++;
G[tot] = { u, w, head[v] }, head[v] = tot++;
}
inline void built(int n, int m) {
int u, v, w;
while(n-- > 1) {
scanf("%d %d %d", &u, &v, &w);
add_edge(u, v, w);
}
for(int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
A.push_back(pair<int, int>(u, v));
Q[u].push_back(pair<int, int>(v, i));
Q[v].push_back(pair<int, int>(u, i));
}
}
inline int find(int x) {
while(x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline void tarjan(int u, int fa) {
for(int i = head[u]; ~i; i = G[i].next) {
edge &e = G[i];
if(e.to == fa) continue;
dist[e.to] = dist[u] + e.w;
tarjan(e.to, u);
vis[e.to] = true;
par[e.to] = u;
}
for(auto &r: Q[u]) {
if(vis[r.first]) ans[r.second] = find(r.first);
}
}
inline void solve(int n, int m) {
init(n);
built(n, m);
tarjan(1, 1);
for(int i = 0; i < m; i++) {
printf("%d\n", dist[A[i].first] + dist[A[i].second] - 2 * dist[ans[i]]);
}
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, n, m;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
go.solve(n, m);
}
return 0;
}

hud 2586 How far away ?的更多相关文章

  1. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  2. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  3. HDU 2586 How far away ?【LCA】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...

  4. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

  5. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  6. hdu - 2586 How far away ?(最短路共同祖先问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...

  7. hdu 2586 How far away ?(LCA - Tarjan算法 离线 模板题)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU 2586 How far away ?(LCA在线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...

  9. hdu 2586 How far away ? 带权lca

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) P ...

随机推荐

  1. tip浮动提示框

    今天工作中碰到要弹出tip浮动提示框,如服务器控件的ToolTip属性. 通过GOOGLE搜到了一个很好用的tip浮动提示框:TipTip jQuery Plugin. 例子如下: <!DOCT ...

  2. 从100PV到1亿级PV网站架构演变

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 一个网站就像一个人,存在一个从小到大的过程.养一个网站和养一个人一样,不同时期需要不 ...

  3. 既约分数-phi

    Description 小明正在学习分数这一章,他想知道对于一个真分数b/a而言,当分母a在[2,N]之间时,存在多少个既约分数,例如当N=4时,有以下几个1/2,1/3,1/4,2/3,3/4这五个 ...

  4. java.sql.SQLException: Access denied for user 'root'

    程序在连接远程 mysql服务器时出错 java.sql.SQLException: Access denied for user 'root'@'192.168.27.129' (using pas ...

  5. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  6. devexpress中应用于girdviw中HtmlDataCellPrepared事件与CellEditorInitialize事件的区别

    HtmlDataCellPrepared 事件为页面展示的时候对页面做的初始化(将id变为name) ​CellEditorInitialize 事件为页面在编辑时(新增.修改)时做的初始化,如将值填 ...

  7. ionic cordova social media sharing plugin

    https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git 从上面的连接下载 插件 1.肯定是要加入 下面的那个文件的吖 从 ...

  8. 教你快速写出多线程Junit单元测试用例 - GroboUtils

    摘自: http://mushiqianmeng.blog.51cto.com/3970029/897786/ 本文出自One Coder博客,转载请务必注明出处: http://www.coderl ...

  9. poj3020

    define     n    the number of  ' * ' define     d    the number of couple of two points define     s ...

  10. sqlserver分区表实践:对时间分区表自动进行管理

    项目问题:有一张日志表,插入和查询为主,每天记录数据为200多万,大小为2G-4G之间.一开始开发人员使用delete语句手动删除,保留7天数据,经常造成阻塞和性能瓶颈.但是如果不删除数据随着表越来越 ...