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 & ...
随机推荐
- LeetCode 543. Diameter of Binary Tree (二叉树的直径)
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- mybatis 参数为list时,校验list是否为空
校验objStatusList 是否为空 <if test="objStatusList != null and objStatusList.size() > 0 "& ...
- hdu 6215 -- Brute Force Sorting(双向链表+队列)
题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...
- 修改Jupyter notebook的启动目录
修改Jupyter notebook的启动目录 1. 在控制台输入以下命令,检查Jupyter notebook的安装目录 jupyter notebook --generate-config 如 ...
- linux学习(十)find命令、Linux文件后缀名、Linux和windows文件互传
一.和find相关的几个搜索命令,了解即可. 1.1 which [root@iZ25lzba47vZ ~]# which ls alias ls='ls --color=auto' /usr/bin ...
- Icon font font face
font-face自定义字体,iconfont就是把各种图片做成字体.iconfont优势: 字体文件小,一般20-50kb: 容易编辑和维护,尺寸和颜色可以用css来控制: 透明完全兼容IE6: ...
- Vue js 的生命周期详解
Vue 实例的生命周期 Vue 实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom→渲染.更新→渲染.卸载等一系列 过程,我们称这是 Vue 的生命周期.通俗说就是 Vue ...
- C# Ioc容器Unity,简单实用
开头先吐槽一下博客园超级不好用,添加图片后就写不动字了,难道是bug 好进入正题,先来说下依赖注入,简单来说就是定义好接口,上层代码调用接口,具体实现通过配置文件方式去指定具体实现类. 首先我们需要通 ...
- activemq Linux下的编译
1.首先下载源码 ,网址:http://www.apache.org/dyn/closer.lua/activemq/activemq-cpp/3.9.4/activemq-cpp-library-3 ...
- C语言控制流语句
title: 2017-10-18控制流 tags: binsearch else-if, shellsort, insertsort grammar_cjkRuby: true --- 前段时间忙着 ...