hdu 2586 How far away ?倍增LCA
hdu 2586 How far away ?倍增LCA
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=2586
思路:
- 针对询问次数多的时候,采取倍增求取LCA,同时跟新距离数组
- 因为
- \(2^{16} > 40000\)
- 所以所以表示祖先的数组dp[][]第二维取到16即可
- 就这道题来说,与比较tarjan比较,稍快一点
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
const int maxn = 40005;
const int maxm = 80005;
struct node {
int to,next,w;
}edges[maxm];
int head[maxn],cnt,dp[maxn][17],dep[maxn],dist[maxn];
inline void addedge(int u, int v, int w) {
edges[cnt].to=v;
edges[cnt].w=w;
edges[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int s, int x) {
dp[s][0]=x;
dep[s]=dep[x]+1;
int t;
for(int i=1;(1<<i)<=dep[s];++i)
dp[s][i]=dp[dp[s][i-1]][i-1];
for(int i=head[s];i!=-1;i=edges[i].next) {
t=edges[i].to;
if(t==x) continue;
dist[t]=dist[s]+edges[i].w;
dfs(t,s);
}
}
inline int lca(int u, int v) {
if(dep[v]>dep[u]) swap(u,v);
for(int i=16;i>=0;--i) {
if((1<<i)<=(dep[u]-dep[v])) {
u=dp[u][i];
}
}
if(u==v) return u;
for(int i=16;i>=0;--i) {
if((1<<i)<=dep[u]&&(dp[v][i]!=dp[u][i])) {
u=dp[u][i];
v=dp[v][i];
}
}
return dp[u][0];
}
inline int slove(int u ,int v) {
int z=lca(u,v);
return dist[u]-2*dist[z]+dist[v];
}
inline void init() {
cnt=0;
memset(head,-1,sizeof(head));
}
int main() {
int t,n,m,u,v,w;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
init();
for(int i=1;i<n;++i) {
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dep[1]=0;//保持dfs的统一,实际dep[1]=1
dist[1]=0;
dfs(1,1);
for(int i=1;i<=m;++i) {
scanf("%d %d",&u,&v);
printf("%d\n",slove(u,v));
}
}
return 0;
}
hdu 2586 How far away ?倍增LCA的更多相关文章
- HDU 2887 Watering Hole(MST + 倍增LCA)
传送门 总算是做上一道LCA的应用题了... 题意:有$n$个牧场, $m$根管道分别连接编号为$u,v$的牧场花费$p_{i}$,在第$i$个牧场挖口井需要花费$w_{i}$,有$P$根管道直接连通 ...
- HDU 2586 How far away ? (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...
- 【HDU 2586 How far away?】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...
- HDU 2586 How far away ? (LCA,Tarjan, spfa)
题意:给定N个节点一棵树,现在要求询问任意两点之间的简单路径的距离,其实也就是最短路径距离. 析:用LCA问题的Tarjan算法,利用并查集的优越性,产生把所有的点都储存下来,然后把所有的询问也储存下 ...
- Hdu 2586 树链剖分求LCA
Code: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> ...
- hdu 2586 How far away?(LCA模板题+离线tarjan算法)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2586 How far away? (LCA模板)
题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhea ...
- hdu 2586 How far away ? ( 离线 LCA , tarjan )
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
随机推荐
- Java基础-流程控制(04)
在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的.也就是说程序的流程对运行结果有直接的影响.所以,我们必须清楚每条语句的执行流程.而且,很多时候我们要通过控制语句的执行顺序来实现我 ...
- Android 6.0运行时权限
一.Runtime Permissions Android 6.0在手机安全方面做的一个处理就是增加了运行时权限(Runtime Permissions). 新的权限机制更好的保护了用户的隐私,Goo ...
- Appium python自动化测试系列之混合app实战(十一)
12.1 什么是混合App 12.1.1 混合app定义 什么是混合app,其实这个不言而喻,我们的app正常来说应该都是native的,但是实际工作中却不是,反正种种原因我们的app会有native ...
- poj3270Cow Sorting(置换+贪心)
Cow Sorting Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7587 Accepted: 2982 Descr ...
- 暑假练习赛 003 B Chris and Road
B - Chris and Road Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144K ...
- 浅谈postgresql的GIN索引(通用倒排索引)
1.倒排索引原理 倒排索引来源于搜索引擎的技术,可以说是搜索引擎的基石.正是有了倒排索引技术,搜索引擎才能有效率的进行数据库查找.删除等操作.在详细说明倒排索引之前,我们说一下与之相关的正排索引并与之 ...
- NFC (Near Filed Communication)
NFC的用途:近场通信(Near Field Communication,NFC),又称近距离无线通信,是一种短距离的高频无线通信技术,允许电子设备之间进行非接触式点对点数据传输(在十厘米内)交换数据 ...
- Pycharm,Python原生IDE?
老套路,安装和使用(Win7x64.JDK神马滴早已装好). 1.安装 网上下下来后就这东西 Next D盘路径 我选择.我喜欢 开装 好慢,以后用光纤 O了 桌面小图标 2.使用 以管理员身份打开软 ...
- 利用Python循环(包括while&for)各种打印九九乘法表
一.for循环打印九九乘法表 #注意:由于缩进在浏览器不好控制,请大家见谅,后续会有图片传入. 1.1 左下角 for i in range(1,10): for j in range(1,i+1): ...
- ldap数据库--ldapsearch,ldapmodify
简单介绍一下ldapsearch命令,在ldap搜索条目时很有用,只要适当调整filter就可以. 命令如下: ldapsearch -h hostname -p port -b baseDN -D ...