hdu 2586(LCA在线ST)
How far away ? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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<=), indicating the number of test cases.
For each test case,in the first line there are two numbers n(<=n<=) and m (<=m<=),the number of houses and the number of queries. The following n- 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(<k<=).The houses are labeled from 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 Sample Output
LCA在线ST:对一颗有根树进行DFS搜索,无论递归还是回溯,每次到达一个节点都将节点的编号记录下来,这样就得到了一条长度为2*n-1的欧拉序列,这样在序列中,从u到v
一定会有u,v的祖先,而不会有u,v祖先节点的祖先,而且u,v之间深度最小的节点就是LCA(u,v),再使用ST算法求RMQ,这样每次查询的时间就能达到O(1)
#include <iostream>
#include <cstdio>
#include <cstring>
#define scan(x) scanf("%d",&x)
#define scan2(x,y) scanf("%d%d",&x,&y)
#define scan3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
const int Max=;
const int E=*;
int head[Max],nex[E],pnt[E],cost[E],edge;
int vex[Max<<],R[Max<<],vis[Max],dis[Max],first[Max],tot;
//!!vex R 长度要Max*2,因为算法特性会生成顶点数两倍的序列
int n;
void Addedge(int u,int v,int c)
{
pnt[edge]=v;cost[edge]=c;
nex[edge]=head[u];head[u]=edge++;
}
void dfs(int u,int deep)
{
vis[u]=;
vex[++tot]=u; //以tot为编号的的节点
first[u]=tot; //u节点的编号为tot
R[tot]=deep; //tot编号节点的深度
for(int x=head[u];x!=-;x=nex[x])
{
int v=pnt[x],c=cost[x];
if(!vis[v])
{
dis[v]=dis[u]+c;
dfs(v,deep+);
vex[++tot]=u;
R[tot]=deep;
}
}
}
int dp[Max<<][];
//!!dp长度要Max*2,,因为算法特性会生成顶点数两倍的序列
void ST(int n) //n是2*n-1
{
int x,y;
for(int i=;i<=n;i++) dp[i][]=i;
for(int j=;(<<j)<=n;j++)
{
for(int i=;i+(<<j)-<=n;i++)
{
x=dp[i][j-];y=dp[i+(<<(j-))][j-];
dp[i][j]=(R[x]<R[y]?x:y);
}
}
}
int RMQ(int l,int r)
{
int k=,x,y;
while((<<(k+))<=r-l+) k++;
x=dp[l][k];y=dp[r-(<<k)+][k];
return (R[x]<R[y])?x:y;
}
int LCA(int u,int v)
{
int x=first[u],y=first[v];
if(x>y) swap(x,y);
int res=RMQ(x,y); //在u,v之间的最小深度节点即为lca
return vex[res];
}
void Init()
{
edge=;
memset(head,-,sizeof(head));
memset(nex,-,sizeof(nex));
memset(vis,,sizeof(vis));
}
int main()
{
int T,Q;
for(scan(T);T;T--)
{
Init();
int u,v,c;
scan2(n,Q);
for(int i=;i<n-;i++)
{
scan3(u,v,c);
Addedge(u,v,c);
Addedge(v,u,c);
}
tot=;dis[]=;
dfs(,);
ST(*n);
while(Q--)
{
scan2(u,v);
int lca=LCA(u,v);
printf("%d\n",dis[u]+dis[v]-*dis[lca]);
}
}
return ;
}
hdu 2586(LCA在线ST)的更多相关文章
- hdu 2586 lca在线算法(朴素算法)
#include<stdio.h> #include<string.h>//用c/c++会爆栈,用g++ac #define inf 0x3fffffff #define N ...
- HDU 2586 LCA
题目大意: 多点形成一棵树,树上边有权值,给出一堆询问,求出每个询问中两个点的距离 这里求两个点的距离可以直接理解为求出两个点到根节点的权值之和,再减去2倍的最近公共祖先到根节点的距离 这是自己第一道 ...
- [hdu 2586]lca模板题(在线+离线两种版本)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 在线版本: 在线方法的思路很简单,就是倍增.一遍dfs得到每个节点的父亲,以及每个点的深度.然后 ...
- HDU 2586 (LCA模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 / \ 2 3 ...
- HDU 2586 ( LCA/tarjan算法模板)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:n个村庄构成一棵无根树,q次询问,求任意两个村庄之间的最短距离 思路:求出两个村庄的LCA,d ...
- (Gym 100685G) Gadget Hackwrench(LCA在线ST)
Gadget Hackwrench time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...
- hdu - 2586 (LCA板子题)
传送门 (这次的英文题面要比上一个容易看多了) (英语蒟蒻的卑微) 又是一个很裸的LCA题 (显然,这次不太容易打暴力咧) (但听说还是有大佬用dfs直接a掉了) 正好 趁这个机会复习一下LCA 这里 ...
- HDU 2586 How far away ?(经典)(RMQ + 在线ST+ Tarjan离线) 【LCA】
<题目链接> 题目大意:给你一棵带有边权的树,然后进行q次查询,每次查询输出指定两个节点之间的距离. 解题分析:本题有多重解决方法,首先,可用最短路轻易求解.若只用LCA解决本题,也有三种 ...
- HDU 2586 How far away ? (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...
随机推荐
- Linux系统man查询命令等级及意义
1:用户命令,可由任何人启动的 2:系统调用,由内核提供的函数 3:库函数 4:设备,/dev目录下的特殊文件 5:文件格式描述,例如/etc/passwd 6:游戏 7:杂项,例如宏命令包.惯例等 ...
- Selenium+Python+Eclipse网页自动化集成环境配置(附简单的测试程序)
最近公司在给我们培训,主要是网页自动化测试的,现在的工作每天都是测APP,刚刚入门,不过,当我看了别人写的bug之后,就觉得不会觉得能够发现bug多么多么的厉害了. 前两周的时间一直在搭建自动化测试的 ...
- Yarn&Mapreduce参数的具体含义和配置参考
Yarn & Mapreduce 参数的具体含义和配置 http://zh.hortonworks.com/blog/how-to-plan-and-configure-yarn-in-hdp ...
- 机器学习中的算法——决策树模型组合之随机森林与GBDT
前言: 决策树这种算法有着很多良好的特性,比如说训练时间复杂度较低,预测的过程比较快速,模型容易展示(容易将得到的决策树做成图片展示出来)等.但是同时,单决策树又有一些不好的地方,比如说容易over- ...
- Django模板格式
变量 {{ 变量名称 }} 块 {% block 块名称 %} 判断 根据布尔值判断: {% if 布尔值 %} 操作 {% endif %} 根据两者是否相等判断: {% ifequals 变量1 ...
- ---ps 命令
ps 的命令真复杂啊! 值得注意的是选项的 -e e 这两个是不同的 -e的选项和x的意思相当 而e 的意思是改变显示栏目内容了 我个人用单字母的比较多 ps f -eo pid,uid,gid,us ...
- SHELL脚本攻略(学习笔记)--2.4 find
转载请注明出处:http://www.cnblogs.com/f-ck-need-u/p/5916657.html 超级强大的find命令. find搜索是从磁盘搜索,而不是从数据库搜索. 2.4 ...
- kafka使用
0: ./sbt update ./sbt package ./sbt assembly-package-dependency 1: 启动ZK: 通过kafka的命令启动:bin/zookeeper- ...
- app.js ejs 转换为html
var express = require('express');var path = require('path');var favicon = require('serve-favicon');v ...
- 7.29 H5学习笔记
常用实体字符小于号 < 大于号 > 和号 & 引号 " 撇号 ' 分 ¢ 镑 £ 日元 ¥ 欧元 € 小节 § 版权 © 注册 ...