题目连接

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. 数据结构-多级指针单链表(C语言)

    偶尔看到大一时候写了一个多级链表,听起来好有趣,稍微整理一下. 稍微注意一下两点: 1.指针是一个地址,他自己也是有一个地址.一级指针(带一个*号)表示一级地址,他自身地址为二级地址.二级指针(带两个 ...

  2. 关于MPEG2中的图像序列和图像组头GOP

    图像序列 图像序列是由图像组构成的,是随机存取段落. sequence_header_code – The sequence_header_code is the bit string ‘000001 ...

  3. 深入探讨ES6生成器

    如果对于ES6生成器不熟悉,请先阅读并运行下http://www.cnblogs.com/linda586586/p/4282359.html里面的代码.当你感觉掌握了基础之后,我们可以深入探讨一些细 ...

  4. linux设置ulimit值永久生效

    小知识的积累,转自 http://hi.baidu.com/moonelf9989/blog/item/1deadf12780fa0c5c2fd789d.html linux 默认打开文件数linux ...

  5. ios NSString常见的字符串操作 分割 查找

    1.NSString *str = [[NSString alloc]init];     //简单粗暴,基本用不到 2.NSString *str = [[NSString alloc]initWi ...

  6. 将Eclipse项目导入Android Studio出现中文乱码的问题

    以前一直以Eclipse开发项目,最近正在研究Android Studio的使用,首先想到到的是将Eclispe项目导入AS. 可以方便查看以前写过的代码,然后出现了中文乱码的问题,通过搜索了一些资料 ...

  7. poj2000

    为了凑今天的数,大水题.不解释了,说来惭愧. #include <stdio.h> int main(){ int n; int i,cnt,j; int tot; while(~scan ...

  8. 十六、Struts2文件上传与下载

    文件上传与下载 1.文件上传前提:<form action="${pageContext.request.contextPath}/*" method="post& ...

  9. 安装 centos7 注意事项

    最近一直没有写博客,把之前的折腾记录写下. 1 下载好镜像文件,刻录光盘. 2  用DVD安装CENTOS7 3 有些处理器不支持Cento7安装,需要重新编译内核文件.我不懂 . 我用的ACER  ...

  10. HTML新特性之一----canvas

    <canvas id="me"></canvas>//申请一个canvas标签    <script>                var c ...