题目链接:

题目

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. linux重要目录

    1.网卡配置文件 /etc/sysconfig/network-scripts/ifcfg-eth* 2.客户端dns配置文件 /etc/resolv.conf 网卡配置文件中的dns配置优先于此文件 ...

  2. HTML5常用标签及特殊字符表

    *http://html5doctor.com/nav*http://html5doctor.com/article*http://html5doctor.com/section*http://htm ...

  3. 课时87. !important(掌握)

    1.什么是important 作用:用于提升某个直接选中标签的选择器中的某个属性的优先级,可以将被指定的属性的优先级提升为最高. 注意点: 1.important只能用于直接选中,不能用于间接选中 p ...

  4. Hadoop系列-HDFS基础

    基本原理 HDFS(Hadoop Distributed File System)是Hadoop的一个基础的分布式文件系统,这个分布式的概念主要体现在两个地方: 数据分块存储在多台主机 数据块采取冗余 ...

  5. php安装redis拓展

    1. 查看是否安装redis库 查看是否安装redis库了.可以通过下面2种方式查看. phpinfo()是否能输出redis的加载信息 在命令行执行`php -m` 输出gd 2. 安装redis库 ...

  6. 20155218 2006-2007-2 《Java程序设计》第2周学习总结

    20155218 2006-2007-2 <Java程序设计>第2周学习总结 教材学习内容总结 java编程风格: java中没有指针的概念,尽管也有数组和对象的引用的概念,但他的管理全部 ...

  7. 【vijos1049】送给圣诞夜的礼品

    题面 描述 当小精灵们把贺卡都书写好了之后.礼品准备部的小精灵们已经把所有的礼品都制作好了.可是由于精神消耗的缘故,他们所做的礼品的质量越来越小,也就是说越来越不让圣诞老人很满意.可是这又是没有办法的 ...

  8. 解决循环里map不被重复覆盖的问题

    参考:https://blog.csdn.net/zyf642112750/article/details/78295113 这样就不会一直重复 项目管理系统 了

  9. 探寻ASP.NET MVC鲜为人知的奥秘(2):与Entity Framework配合,让异步贯穿始终

    Why 在应用程序,尤其是互联网应用程序中,性能一直是很多大型网站的困扰,由于Web2.0时代的到来,人们更多的把应用程序从C/S结构迁移到B/S结构,这样会带来客户端轻量,部署.试试方便快捷等优势, ...

  10. 如何运用 Powershell 修改Office365和AD账户

    这段时间需要大量地修改AD用户的一些属性,例如邮件,UPN,登录名等等,以便和Office365的登录账号保持一致.写了个简单脚本进行批量修改. #Import AD ModuleImport-Mod ...