题目链接:

题目

How far away ?

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

问题描述

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.

输入

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.

输出

For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.

样例

input

2

3 2

1 2 10

3 1 15

1 2

2 3

2 2

1 2 100

1 2

2 1

output

10

25

100

100

题意

给你一颗树,问两点间距离

题解

离线求每个点的深度,则距离为dep[u]+dep[v]-2*dep[lca(u,v)];

代码

#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#define mp make_pair
#define X first
#define Y second
using namespace std; const int maxn = 4e4+10;
const int maxm = 18; int n, m; vector<pair<int, int> > G[maxn];
int dep[maxn],dep2[maxn],anc[maxn][maxm];
void dfs(int u,int fa,int d,int d2) {
dep[u] = d, dep2[u] = d2;
anc[u][0] = fa;
for (int j = 1; j < maxm; j++) {
int f = anc[u][j - 1];
anc[u][j] = anc[f][j - 1];
}
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i].X, w = G[u][i].Y;
if (v == fa) continue;
dfs(v, u, d + 1, d2 + w);
}
} int Lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = maxm - 1; i >= 0; i--) {
if (dep[anc[u][i]] >= dep[v]) {
u = anc[u][i];
}
}
if (u == v) return u;
for (int i = maxm - 1; i >= 0; i--) {
if (anc[u][i] != anc[v][i]) {
u = anc[u][i], v = anc[v][i];
}
}
return anc[u][0];
} void init() {
for (int i = 0; i <= n; i++) G[i].clear();
memset(anc, 0, sizeof(anc));
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &m);
init();
for (int i = 0; i < n - 1; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
G[u].push_back(mp(v, w));
G[v].push_back(mp(u, w));
}
dfs(1, 0,0,0);
while (m--) {
int u, v;
scanf("%d%d", &u, &v);
int lca = Lca(u, v);
printf("%d\n", dep2[u] + dep2[v] - 2 * dep2[lca]);
}
}
return 0;
}

HDU 5286 How far away ? lca的更多相关文章

  1. hdu 5286 How far away ? tarjan/lca

    How far away ? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  2. HDU 2586 How far away ? (LCA)

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

  3. HDU 2586 How far away ? (LCA,Tarjan, spfa)

    题意:给定N个节点一棵树,现在要求询问任意两点之间的简单路径的距离,其实也就是最短路径距离. 析:用LCA问题的Tarjan算法,利用并查集的优越性,产生把所有的点都储存下来,然后把所有的询问也储存下 ...

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

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

  5. hdu 2586 How far away ?倍增LCA

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

  6. 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 ...

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

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

  8. hdu 2586 How far away ? 带权lca

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

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

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

随机推荐

  1. JQuery简单总结(思维导图)

  2. PHP-掌握基本的分布式架构思想

    虽然说写PHP目前都是接触的业务代码,发现写久了,也要熟悉相应的架构 在高并发,高可用的系统下,都是使用高性能的分布式架构,最近在学习相关知识 分享一张图片: 欢迎关注公众号[phper的进阶之路], ...

  3. MySQLFront导入SQL文件报#1113错误解决

  4. 详解Linux运维工程师高级篇(大数据安全方向).

    hadoop安全目录: kerberos(已发布) elasticsearch(已发布)http://blog.51cto.com/chenhao6/2113873 knox oozie ranger ...

  5. QEMU 模拟运行 VxWorks 6.6

    QEMU 模拟运行 VxWorks 6.6 项目简介 本项目是在 Windows 系统编译运行 X86 平台 VxWorks 6.6 系统,使用的模拟软件是 qemu for Windows Host ...

  6. Go 跨域请求问题

    在使用go语言写测试服务的时候遇到了前端跨域请求问题,只需在go中加入 w.Header().Set("Access-Control-Allow-Origin", "*& ...

  7. 两组数据的均值是否具有显著差异的T检验

    最近在做分析的时候,遇到了T检验,然而对于没有统计学背景的人来说完全不知如何下手 当然了,遇到问题第一反应就是百度. 果然百度出来了很多链接,当时第一次直接选择了用Excel去做T检验.下面是源数据 ...

  8. 20155230 《Java程序设计》实验五 Java网络编程及安全

    20155230 <Java程序设计>实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验1 两人一组结对编程 ...

  9. 20155328 《Java程序设计》 实验二(Java面向对象程序设计) 实验报告

    20155328 <Java程序设计> 实验二(Java面向对象程序设计) 实验报告 单元测试 一.单元测试和TDD 编程时需理清思路,将编程需求等想好,再开始编.此部分可用伪代码实现. ...

  10. 第五周 mybash的实现

    第五周 mybash的实现 1. 使用fork,exec,wait实现mybash 2. 写出伪代码,产品代码和测试代码 3. 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 1. for ...