How far away ?

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

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
 
题意:
n个点组成的一棵树,2点之间有长度。m次查询,问x到y上每个点只能走一次的最近的距离是多少。
思路:
是一棵树,又x到y的路径上每个点只能走一次,dfs时记录当前节点到根的距离,然后对于查询x,y,求出lca(x,y),答案就是dis[x] + dis[y] - 2 * dis[lca(x,y)];
 
/*
* Author: sweat123
* Created Time: 2016/7/13 8:53:48
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int to;
int val;
int next;
}edge[MAXN*];
int dp[MAXN*][],n,m,ind,pre[MAXN],vis[MAXN];
int dfn[MAXN*],first[MAXN],ver[MAXN*],tot;
ll dis[MAXN];
//dfn 表示深度 first第一次出现这个点的下标 dis长度 ver先序访问的节点编号 tot编号个数
void add(int x,int y,int z){
edge[ind].to = y;
edge[ind].val = z;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt,int dep){
vis[rt] = ;
ver[++tot] = rt;
dfn[tot] = dep;
first[rt] = tot;
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t]){
dis[t] = dis[rt] + edge[i].val;
dfs(t,dep+);
ver[++tot] = rt;//先序访问
dfn[tot] = dep;
}
}
}
void rmq(){
for(int i = ; i <= tot; i++){
dp[i][] = i;
}
for(int i = ; i < ; i++){
for(int j = ; j + ( << i) - <= tot; j++){
if(dfn[dp[j][i-]] > dfn[dp[j+(<<(i-))][i-]]){
dp[j][i] = dp[j+(<<(i-))][i-];
} else {
dp[j][i] = dp[j][i-];
}
}
}
}
int askrmq(int x,int y){
x = first[x];
y = first[y];
if(x > y)swap(x,y);
int k = (int)(log(y - x + ) * 1.0 / log(2.0));
int l = dp[x][k];
int r = dp[y-(<<k)+][k];
if(dfn[l] > dfn[r])return r;
else return l;
}
int main(){
int t,flag = ;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
ind = ;
tot = ;
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
memset(pre,-,sizeof(pre));
for(int i = ; i < n; i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z),add(y,x,z);
}
dfs(,);
rmq();
if(flag)puts("");
flag = ;
while(m--){
int x,y;
scanf("%d%d",&x,&y);
int tp = ver[askrmq(x,y)];
ll ans = dis[x] - dis[tp] + dis[y] - dis[tp];
printf("%lld\n",ans);
}
}
return ;
}

hdu2586 LCA的更多相关文章

  1. poj1330+hdu2586 LCA离线算法

    整整花了一天学习了LCA,tarjan的离线算法,就切了2个题. 第一题,给一棵树,一次查询,求LCA.2DFS+并查集,利用深度优先的特点,回溯的时候U和U的子孙的LCA是U,U和U的兄弟结点的子孙 ...

  2. hdu2586 lca倍增法

    倍增法加了边的权值,bfs的时候顺便把每个点深度求出来即可 #include<iostream> #include<cstring> #include<cstdio> ...

  3. hdu2586(lca模板 / tarjan离线 + RMQ在线)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意: 给出一棵 n 个节点的带边权的树, 有 m 个形如 x y 的询问, 要求输出所有 x, ...

  4. HDU2586(LCA应用:在带权树中求任意两点之间的距离)

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

  5. hdu2586 LCA带边权的Targan算法

    bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=2586 #include<bits/stdc++.h> using names ...

  6. LCA在线算法(hdu2586)

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

  7. LCA 离线的Tarjan算法 poj1330 hdu2586

    LCA问题有好几种做法,用到(tarjan)图拉算法的就有3种.具体可以看邝斌的博客.http://www.cnblogs.com/kuangbin/category/415390.html 几天的学 ...

  8. HDU2586 How far away ?(LCA模板题)

    题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:详细解说 传送门 代码例 ...

  9. 模板倍增LCA 求树上两点距离 hdu2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 课上给的ppt里的模板是错的,wa了一下午orz.最近总是被坑啊... 题解:树上两点距离转化为到根的距离 ...

随机推荐

  1. DEDE建站之图片标签技巧指南

    做dede站的时候,曾经遇到很苦恼的事情,就是给图片集添加了一个网上下载下来的特效,需要给图片的链接上添加一个rel属性,供JS调用以达到那种特效.但是当时只知道dede的图片链接标签是[field: ...

  2. 总结一下Android中主题(Theme)的正确玩法

    在AndroidManifest.xml文件中有<application android:theme="@style/AppTheme">,其中的@style/AppT ...

  3. IOS RunLoop浅析 一

    RunLoop犹如其名循环. RunLoop 中有多重模式. 在一个“时刻”只能值执行一种模式. 因此在使用RunLoop时要注意所实现的效果有可能不是你想要的. 在这里用NSTimer展示一下Run ...

  4. spring mvc生成注册验证码

    通过Spring MVC为系统添加验证码 1:布局登陆页面,用户名,密码,填写验证码的文本框,及验证码的图片及点击换图 <%@ taglib prefix="c" uri=& ...

  5. C# Combobox 设置 value

    因为ComboxItem是Object对象,而控件自身没有Value属性.所以,需要自定义一个类,用其对象来存储Text,Value. public class ComboxItem    {     ...

  6. java实现REST方式的webService

    一. 简介 WebService有两种方式,一是SOAP方式,二是REST方式.SOAP是基于XML的交互,WSDL也是一个XML文档, 可以使用WSDL作为SOAP的描述文件:REST是基于HTTP ...

  7. class.c 添加中文注释(2)

    /* Class Device Stuff */ int class_device_create_file(struct class_device * class_dev, const struct ...

  8. TCP协议

    TCP是一个面向连接的协议,在发送数据之前,必须在双方之间建立一条连接. TCP首部 TCP数据封装在IP数据报中 TCP包首部 下面简单说明部分字段的作用: 端口号:通讯双方由IP地址和端口号标识. ...

  9. Xstream学习资料

    java中有关xml操作的,我们项目中首推Xstream.至于原因不说了.跟着大众的脚步走应该没错的.有关Xstream的文档如下. 官方文档 XStream完美转换XML.JSON XStream实 ...

  10. [WPF系列]-Adorner

      简介 通常我们想对现有的控件,做些修饰时我们就会想到一个装饰模式.WPF中也提供了这样的实现思路:通过将Adorner添加到AdornerLayer中来实现装饰现有控件的效果.如图示:   本来T ...