【HDU 2586】LCA模板
在一棵树上 求2个点的最短距离。那么首先利用LCA找到2个点的近期公共祖先
公式:ans = dis(x) + dis(y) - 2 * dis(lca(x,y))
这里的dis(x)指的上x距离根节点的距离
注意一些细节方面,比方数组的越界问题:
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 45555;
struct Edge{
int to;
LL dist;
Edge(int to,LL dist):to(to),dist(dist){};
};
int n,m;
int deep[maxn],pa[maxn][22];
LL dis[maxn];
vector<Edge>G[maxn];
void init(){
memset(pa,-1,sizeof(pa));
for(int i = 1; i <= n; i++) G[i].clear();
}
//----------------LAC---------------------------
void dfs(int pos,int d,LL dist){
//printf("[%d %d]\n",pos,dist);
deep[pos] = d;
dis[pos] = dist;
int Size = G[pos].size();
for(int i = 0; i < Size; i++)
dfs(G[pos][i].to,d + 1,dist + G[pos][i].dist);
}
void lca_init(){
for(int j = 1; (1 << j) <= n; j++)
for(int i = 1; i <= n; i++)
if(pa[i][j - 1] != -1)
pa[i][j] = pa[pa[i][j - 1]][j - 1];
}
int lca(int a,int b){
if(a == b)
return a;
if(deep[a] < deep[b]) swap(a,b);
int i;
for(i = 0;(1 << i) <= deep[a]; i++);
for(int j = i; j >= 0; j--)
if(pa[a][j] != -1 && deep[pa[a][j]] >= deep[b])
a = pa[a][j];
if(a == b)
return b;
for(int j = i; j >= 0; j--)
if(pa[a][j] != -1 && deep[pa[a][j]] != deep[pa[b][j]]){
a = pa[a][j];
b = pa[b][j];
}
return pa[a][0];
}
//-------------------------------------------------
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int x,y,z;
init();
for(int i = 0; i < n - 1; i++){
scanf("%d%d%d",&x,&y,&z);
G[x].push_back(Edge(y,z));
pa[y][0] = x;
}
for(int i = 1; i <= n; i++)if(pa[i][0] == -1){
dfs(i,0,0);
break;
}
lca_init();
for(int i = 0; i < m; i++){
scanf("%d%d",&x,&y);
LL ans = dis[x] + dis[y] - 2 * dis[lca(x,y)];
printf("%I64d\n",ans);
}
}
return 0;
}
【HDU 2586】LCA模板的更多相关文章
- HDU 2586 (LCA模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 / \ 2 3 ...
- [hdu 2586]lca模板题(在线+离线两种版本)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 在线版本: 在线方法的思路很简单,就是倍增.一遍dfs得到每个节点的父亲,以及每个点的深度.然后 ...
- HDU 2586 ( LCA/tarjan算法模板)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:n个村庄构成一棵无根树,q次询问,求任意两个村庄之间的最短距离 思路:求出两个村庄的LCA,d ...
- hdu 2586(LCA在线ST)
How far away ? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): A ...
- HDU 2586 LCA
题目大意: 多点形成一棵树,树上边有权值,给出一堆询问,求出每个询问中两个点的距离 这里求两个点的距离可以直接理解为求出两个点到根节点的权值之和,再减去2倍的最近公共祖先到根节点的距离 这是自己第一道 ...
- hdu - 2586 (LCA板子题)
传送门 (这次的英文题面要比上一个容易看多了) (英语蒟蒻的卑微) 又是一个很裸的LCA题 (显然,这次不太容易打暴力咧) (但听说还是有大佬用dfs直接a掉了) 正好 趁这个机会复习一下LCA 这里 ...
- hdu 2586 lca在线算法(朴素算法)
#include<stdio.h> #include<string.h>//用c/c++会爆栈,用g++ac #define inf 0x3fffffff #define N ...
- HDU 2586 + HDU 4912 最近公共祖先
先给个LCA模板 HDU 1330(LCA模板) #include <cstdio> #include <cstring> #define N 40005 struct Edg ...
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- HDU 2586——How far away ?——————【LCA模板题】
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- 在Windows上以服务方式运行 MSOPenTech/Redis
ServiceStack.Redis 使用教程里 提到Redis最好还是部署到Linux下去,Windows只是用来做开发环境,现在这个命题发生改变了,在Windows上也可以部署生产环境的 Redi ...
- OpenCV使用二维特征点(Features2D)和单映射(Homography)寻找已知物体
使用二维特征点(Features2D)和单映射(Homography)寻找已知物体 目标 在本教程中我们将涉及以下内容: 使用函数 findHomography 寻找匹配上的关键点的变换. 使用函数 ...
- VS2010 OpenCL运行环境配置
1.安装AMD GPU显卡驱动(安装后重启) 13-12_win7_win8_64_dd_ccc_whql.exe 2.安装AMD OpenCL SDK(安装后重启) AMD-APP-SDK-v2.9 ...
- oracle分析函数之windowing_clause--rows
Some analytic functions allow the windowing_clause. In the listing of analytic functions at the end ...
- Unity3d笔试题大全
1. [C#语言基础]请简述拆箱和装箱. 答: 装箱操作: 值类型隐式转换为object类型或由此值类型实现的任何接口类型的过程. 1.在堆中开辟内存空间. 2.将值类型的数据复制到堆中. ...
- 虚机启动失败-Event 1069
Failover cluster中的一台虚机启动失败. 报错如下: 文字信息关键字如下: Event 1069; Event 21502; 0x80004005; Cluster resource ' ...
- Pairwise
FCC题目:找到你的另一半 有一个能力数组[7,9,11,13,15],按照最佳组合值为20来计算,只有7+13和9+11两种组合.而7在数组的索引为0,13在数组的索引为3,9在数组的索引为1,11 ...
- 飘逸的python - 不使用keyword,求和1+2+…+n
依据题意,我们不能用到if/else/for/while等keyword. --------------思考中------------ 思路: 1.用递归实现循环 2.递归的终止条件不用if怎么推断呢 ...
- Python MongoDB Spatial Query
//引入Pymongo >>> from pymongo import MongoClient,GEO2D // 链接数据库gis >>> db = MongoCl ...
- Inside GDALAllRegister之三: 注册指定驱动
现在来仔细分析如何注册一个驱动的代码,看下面代码: #ifdef FRMT_vrt GDALRegister_VRT(); #endif 编译时指定或者取消FRMT_vrt,可以控制这条语句是否编译到 ...