HDU2586.How far away ?-LCA(在线ST算法)

How far away ?

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

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(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

Source

ECJTU 2009 Spring Contest

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 const int N=40000+10;
 7 const int M=25;
 8 int dp[2*N][M];
 9 bool vis[N];
10 struct edge{
11     int u,v,w,next;
12 }e[2*N];
13 int tot,head[N];
14 inline void add(int u,int v,int w,int &k){
15     e[k].u=u;e[k].v=v;e[k].w=w;
16     e[k].next=head[u];head[u]=k++;
17     u=u^v;v=u^v;u=u^v;
18     e[k].u=u;e[k].v=v;e[k].w=w;
19     e[k].next=head[u];head[u]=k++;
20 }
21 int ver[2*N],R[2*N],first[N],dir[N];
22 void dfs(int u,int dep){
23     vis[u]=true;ver[++tot]=u;first[u]=tot;R[tot]=dep;
24     for(int k=head[u];k!=-1;k=e[k].next)
25     if(!vis[e[k].v]){
26         int v=e[k].v,w=e[k].w;
27         dir[v]=dir[u]+w;
28         dfs(v,dep+1);
29         ver[++tot]=u;R[tot]=dep;
30     }
31 }
32 void ST(int n){
33     for(int i=1;i<=n;i++)
34         dp[i][0]=i;
35     for(int j=1;(1<<j)<=n;j++){
36         for(int i=1;i+(1<<j)-1<=n;i++){
37             int a=dp[i][j-1],b=dp[i+(1<<(j-1))][j-1];
38             dp[i][j]=R[a]<R[b]?a:b;
39         }
40     }
41 }
42 int RMQ(int l,int r){
43     int k=0;
44     while((1<<(k+1))<=r-l+1)k++;
45     int a=dp[l][k],b=dp[r-(1<<k)+1][k];
46     return R[a]<R[b]?a:b;
47 }
48 int LCA(int u,int v){
49     int x=first[u],y=first[v];
50     if(x>y)swap(x,y);
51     int res=RMQ(x,y);
52     return ver[res];
53 }
54 int main(){
55     int cas;
56     scanf("%d",&cas);
57     while(cas--){
58         int n,q,num=0;
59         scanf("%d%d",&n,&q);
60         memset(head,-1,sizeof(head));
61         memset(vis,false,sizeof(vis));
62         for(int i=1;i<n;i++){
63             int u,v,w;
64             scanf("%d%d%d",&u,&v,&w);
65             add(u,v,w,num);
66         }
67         tot=0;dir[1]=0;
68         dfs(1,1);
69         ST(2*n-1);
70         while(q--){
71             int u,v;
72             scanf("%d%d",&u,&v);
73             int lca=LCA(u,v);
74             printf("%d\n",dir[u]+dir[v]-2*dir[lca]);
75         }
76     }
77     return 0;
78 }

原文地址:https://www.cnblogs.com/ZERO-/p/9114403.html

时间: 2024-08-08 18:00:53

HDU2586.How far away ?-LCA(在线ST算法)的相关文章

LCA在线算法ST算法

求LCA(最近公共祖先)的算法有好多,按在线和离线分为在线算法和离线算法. 离线算法有基于搜索的Tarjan算法较优,而在线算法则是基于dp的ST算法较优. 首先说一下ST算法. 这个算法是基于RMQ(区间最大最小值编号)的,不懂的可以这里学习一些 而求LCA就是把树通过深搜得到一个序列,然后转化为求区间的最小编号. 比如说给出这样一棵树. 我们通过深搜可以得到这样一个序列: 节点ver 1 3 1 2 5 7 5 6 5 2 4 2 1 (先右后左) 深度R 1 2 1 2 3 4 3 4 3

LCA在线算法详解

LCA(最近公共祖先)的求法有多种,这里先介绍第一种:在线算法. 声明一下:下面的内容参考了http://www.cnblogs.com/scau20110726/archive/2013/05/26/3100812.html. 在线算法就是利用了DFS和RMQ两种算法,它先是预处理好所有情况,然后根据输入输出答案,在输入比较多的时候用比较好. 上面两张图介绍了在线算法的做法,要理解并不难,下面附上实现代码: 1 /******************************* 2 dfs实现代

hihocoder1069最近公共祖先&#183;三(LCA在线算法--DFS+RMQ-ST)

树上任意两点的最近祖先,必定就是这两个节点的最短路径上深度最小的那个点. 例如:下图中,节点7和5,其最短路径为7--4--1--5, 这条路径上深度最小的点为节点1,其深度为1.节点1即为节点7和5的LCA. 因此,要找到任意两个节点的LCA,只需要先找到上述最短路径,再找到最短路径中深度最小的点.而这下面所述LCA在线算法所做的事. LCA在线算法描述(以上图为例): 1.获得“最短路径”(并不是真正的一条路径,包含其他节点,但不影响算法的正确性) 采用DFS遍历整棵树,得到以下数据: (1

学习笔记 ST算法

[引子]RMQ (Range Minimum/Maximum Query)问题: 对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就是说,RMQ问题是指求区间最值的问题. {方法} 1.朴素(即搜索),O(n)-O(qn) online. 2.线段树,O(n)-O(qlogn) online. 3.ST(实质是动态规划),O(nlogn)-O(q) online. ST算法(Sparse Table),以求最大值为例,设d[i,

最近公共祖先(LCA)---tarjan算法

LCA(最近公共祖先).....可惜我只会用tarjan去做 真心感觉tarjan算法要比倍增算法要好理解的多,可能是我脑子笨吧略略略 最近公共祖先概念:在一棵无环的树上寻找两个点在这棵树上深度最大的公共的祖先节点,也就是离这两个点最近的祖先节点. 最近公共祖先的应用:求解两个有且仅有一条确定的最短路径的路径 举个例子吧,如下图所示4和5的最近公共祖先是2,5和3的最近公共祖先是1,2和1的最近公共祖先是1. 这就是最近公共祖先的基本概念了,那么我们该如何去求这个最近公共祖先呢? Tarjan介

[总结]RMQ问题&amp;ST算法

目录 一.ST算法 二.ST算法の具体实现 1. 初始化 2. 求出ST表 3. 询问 三.例题 例1:P3865 [模板]ST表 例2:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 一.ST算法 ST算法(Sparse Table Algorithm)是用于解决RMQ问题(区间最值问题,即Range Maximum/Minimum Question)的一种著名算法. ST算法能在复杂度为\(O(NlogN)\)的预处理后,以\(O(1)\)的复杂度在线处理序列区

ST算法

作用:ST算法是用来求解给定区间RMQ的最值,本文以最小值为例 举例: 给出一数组A[0~5] = {5,4,6,10,1,12},则区间[2,5]之间的最值为1. 方法:ST算法分成两部分:离线预处理 (nlogn)和 在线查询(O(1)).虽然还可以使用线段树.树状链表等求解区间最值,但是ST算法要比它们更快,而且适用于在线查询. (1)离线预处理:运用DP思想,用于求解区间最值,并保存到一个二维数组中. (2)在线查询:对给定区间进行分割,借助该二维数组求最值 具体解释: (1)离线预处理

RMQ问题ST算法

1. 概述 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j之间的最小/大值.这两个问题是在实际应用中经常遇到的问题,下面介绍一下解决这两种问题的比较高效的算法.当然,该问题也可以用线段树(也叫区间树)解决,算法复杂度为:O(N)~O(logN),这里我们暂不介绍. 2.RMQ算法 对于该问题,最容易想到的解决方案是遍历,复杂度是O(n).但当数据量

【RMQ】【ST算法】【模板】士兵杀敌(三)

描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比较,计算出两个人的杀敌数差值,用这种方法一方面能鼓舞杀敌数高的人,另一方面也算是批评杀敌数低的人,起到了很好的效果. 所以,南将军经常问军师小工第i号士兵到第j号士兵中,杀敌数最高的人与杀敌数最低的人之间军功差值是多少. 现在,请你写一个程序,帮小工回答南将军每次的询问吧. 注意,南将军可能询问很多次. 输入 只有一组测试数据 第一行是两个整数N,Q,其中N表示士兵的总数.Q表示南将军