Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 13390 Accepted Submission(s): 5018

Problem 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(0k<=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

【题解】



设p[i][j]表示从i时间戳开始2^j个时间戳范围内高度最小的一个节点的编号;

//这个东西可以用求RMQ的方法搞出来。

找到要求的两个节点第一次出现的时间戳x,y;

并确保x小于y;

p[x][k]和p[y-k+1][k])

取这两个节点中高度较小的就是x和y的最近公共祖先了;

其中k是y-x+1这个距离,要用2的多少次方表示;

找到祖先后输出dis[x]+dis[y]-2*dis[LCA];就是距离了。

这个距离是树上的最短距离。还是很有用的。可以扩展下;

#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm> using namespace std; const int MAXN = 50000;
const int MAX = 16; vector <int> son[MAXN],w[MAXN];
int n,p[MAXN*4][MAX+5],dep[MAXN],pre[MAX+5],m,cnt,fir[MAXN],shall[MAXN*4];
long long dis[MAXN]; void input(int &r)
{
char t = getchar();
while (!isdigit(t)) t = getchar();
r = 0;
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
} void dfs(int x,int f)
{
dep[x] = dep[f] + 1;
p[++cnt][0] = x;
fir[x] = cnt;
int len = son[x].size();
for (int i = 0; i <= len - 1; i++)
{
int y = son[x][i];
if (y != f)
{
dis[y] = dis[x] + w[x][i];
dfs(y, x);
p[++cnt][0] = x;
}
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
pre[0] = 1;
for (int i = 1; i <= MAX; i++)
pre[i] = pre[i - 1] << 1;
int T;
input(T);
while (T--)
{
cnt = 0;
input(n); input(m);
for (int i = 1; i <= n; i++)
son[i].clear(),w[i].clear();
for (int i = 1; i <= n - 1; i++)
{
int x, y, z;
input(x); input(y); input(z);
son[x].push_back(y);
w[x].push_back(z);
son[y].push_back(x);
w[y].push_back(z);
}
dis[1] = 0;
dfs(1, 0);
shall[1] = 0;
shall[2] = 1;
int top = 2;
for (int i = 3; i <= cnt; i++)
{
if (i == pre[top])
shall[i] = shall[i - 1] + 1;
else
shall[i] = shall[i - 1];
}
for (int j = 1;j <= MAX;j++)
for (int i = 1; i +pre[j-1]<= cnt; i++)
{
int a = p[i][j - 1];
int b = p[i + pre[j - 1]][j - 1];
if (dep[a] < dep[b])
p[i][j] = a;
else
p[i][j] = b;
}
for (int i = 1; i <= m; i++)
{
int x, y,px,py;
input(x); input(y);
px = x, py = y;
x = fir[x], y = fir[y];
if (x > y)
swap(x, y);
int k = shall[y - x + 1];
int lca;
int ll, rr;
ll = p[x][k];
rr = p[y - pre[k] + 1][k];
if (dep[ll] < dep[rr])
lca = ll;
else
lca = rr;
printf("%I64d\n", dis[px] + dis[py] - 2 * dis[lca]);
}
}
return 0;
}

【37.48%】【hdu 2587】How far away ?(3篇文章,3种做法,LCA之ST算法(RMQ))的更多相关文章

  1. 【37.48%】【hdu 2587】How far away ?(3篇文章,3种做法,LCA之Tarjan算法)

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  2. 【37.48%】【hdu 2587】How far away ?(3篇文章,3种做法,LCA之树上倍增)

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  3. HDU 1248 寒冰王座 (水题的N种做法!)(含完全背包)

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. HDU 5443 The Water Problem (ST算法)

    题目链接:HDU 5443 Problem Description In Land waterless, water is a very limited resource. People always ...

  5. HDU 3183 - A Magic Lamp - [RMQ][ST算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...

  6. HDU 5289 Assignment (ST算法区间最值+二分)

    题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...

  7. Hdu1874 畅通工程续 2017-04-12 18:37 48人阅读 评论(0) 收藏

    畅通工程续 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  8. HDU 2587 - 很O_O的汉诺塔

    看题传送门 吐槽题目 叫什么很O_O的汉诺塔我还@.@呢. 本来是想过一段时间在来写题解的,不过有人找我要. 本来排名是第8的.然后搞了半天,弄到了第五.不过代码最短~ 截止目前就9个ID过,小小的成 ...

  9. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

随机推荐

  1. python 代码中的类和对象

  2. sql —— between

    BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围. 原表: 执行查询: 上面就可以搜索出得分为80~90的学生了,包含80,也包含90.

  3. 【JZOJ4884】【NOIP2016提高A组集训第12场11.10】图的半径

    题目描述 mhy12345学习了树的直径,于是开始研究图的半径,具体来说,我们需要在图中选定一个地方作为中心,其中这个中心有可能在路径上. 而这个中心的选址需要能够使得所有节点达到这个中心的最短路里面 ...

  4. 笔记:在 Windows 10 WSL Ubuntu 18.04 安装 Odoo12 (2019-06-09)

    笔记:在 Windows 10 WSL Ubuntu 18.04 安装 Odoo12 原因 为了和服务器一样的运行环境. 使用 Ubuntu 运行 Odoo 运行更快. 方便使用 Windows 10 ...

  5. 阿里云异构计算发布:轻量级GPU云服务器实例VGN5i

    阿里云发布了国内首个公共云上的轻量级GPU异构计算产品——VGN5i实例,该实例打破了传统直通模式的局限,可以提供比单颗物理GPU更细粒度的服务,从而让客户以更低成本.更高弹性开展业务.适用于云游戏. ...

  6. XAML 特效

    <Window x:Class="WpfApp5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/200 ...

  7. 阿里云POLARDB如何助力轻松筹打造5亿用户信赖的大病筹款平台?

    轻松筹首创了“大病救助”模式,帮助了众多病患在第一时间解決了医疗资金等问题,为了从源头解决了医疗资金问题.而在轻松筹这样全球5.5亿用户信赖的大病筹款平台的背后,是日益增长的各种数据.面对这样数据量所 ...

  8. 大侦探福老师——幽灵Crash谜踪案

    闲鱼Flutter技术的基础设施已基本趋于稳定,就在我们准备松口气的时候,一个Crash却异军突起冲击着我们的稳定性防线!闲鱼技术火速成立侦探小组执行嫌犯侦查行动,经理重重磨难终于在一个隐蔽的角落将其 ...

  9. LeetCode69 Sqrt(x)

    题意: Implement int sqrt(int x). Compute and return the square root of x.(Medium) 分析: 二分搜索套路题,不能开方开尽的时 ...

  10. python常量和变量

    1.1 常量 常量是内存中用于保存固定值的单元,在程序中常量的值不能发生改变:python并没有命名常量,也就是说不能像C语言那样给常量起一个名字. python常量包括:数字.字符串.布尔值.空值: ...