HDU2586---How far away ?(lca算法)
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.
LCA_倍增是LCA的在线算法,时间和空间复杂度分别是O((n+q)log n)和O(n log n)。
对于这个算法,我们从最暴力的算法开始:
①如果a和b深度不同,先把深度调浅,使他变得和浅的那个一样
②现在已经保证了a和b的深度一样,所以我们只要把两个一起一步一步往上移动,直到他们到达同一个节点,也就是他们的最近公共祖先了。
/*
author:gsw
data:2018.04.30
link:http://acm.hdu.edu.cn/showproblem.php?pid=2586
account:tonysave
*/
#define ll long long
#define IO ios::sync_with_stdio(false);
#define maxn 40005 #include<vector>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int t,n,m;
class Node{
public:
int nex,len;
};
vector<Node> tree[maxn];
int dis[maxn];int deep[maxn];
int fa[maxn]; void init()
{
for(int i=;i<n;i++)
tree[i].clear();
memset(dis,,sizeof(dis));
memset(deep,,sizeof(deep));
memset(fa,,sizeof(fa));
}
Node ne;
void dfs(int num,int faa)
{
for(int i=;i<tree[num].size();i++)
{
ne=tree[num][i];
if(ne.nex!=faa)
{
fa[ne.nex]=num;
deep[ne.nex]=deep[num]+;
dis[ne.nex]=dis[num]+ne.len;
dfs(ne.nex,num);
}
}
}
int lca(int a,int b)
{
if(deep[a]>deep[b])
swap(a,b);
while(deep[b]>deep[a])
b=fa[b];
while(a!=b)
a=fa[a],b=fa[b];
return a;
} int main()
{
int a,b,c;Node tem;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<n-;i++)
{
scanf("%d%d%d",&a,&b,&c);
tem.nex=b;tem.len=c;
tree[a].push_back(tem);
tem.nex=a;
tree[b].push_back(tem);
}
dfs(,);
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
int ans=lca(a,b);
ans=dis[a]+dis[b]-*dis[ans];
printf("%d\n",ans);
}
}
}
HDU2586---How far away ?(lca算法)的更多相关文章
- LCA算法
LCA算法: LCA(Least Common Ancestor),顾名思义,是指在一棵树中,距离两个点最近的两者的公共节点.也就是说,在两个点通往根的道路上,肯定会有公共的节点,我们就是要求找到公共 ...
- 【图论】tarjan的离线LCA算法
百度百科 Definition&Solution 对于求树上\(u\)和\(v\)两点的LCA,使用在线倍增可以做到\(O(nlogn)\)的复杂度.在NOIP这种毒瘤卡常比赛中,为了代码的效 ...
- LCA算法解析-Tarjan&倍增&RMQ
原文链接http://www.cnblogs.com/zhouzhendong/p/7256007.html UPD(2018-5-13) : 细节修改以及使用了Latex代码,公式更加美观.改的过程 ...
- LCA算法倍增算法(洛谷3379模板题)
倍增(爬树)算法,刚刚学习的算法.对每一个点的父节点,就记录他的2k的父亲. 题目为http://www.luogu.org/problem/show?pid=3379 第一步先记录每一个节点的深度用 ...
- LCA 算法(二)倍增
介绍一种解决最近公共祖先的在线算法,倍增,它是建立在任意整数的二进制拆分之上. 代码: //LCA:Doubly #include<cstdio> #define swap(a, ...
- LCA 算法(一)ST表
介绍一种解决最近公共祖先的在线算法,st表,它是建立在线性中的rmq问题之上. 代码: //LCA: DFS+ST(RMQ) #include<cstdio> #include&l ...
- LCA算法总结
LCA问题(Least Common Ancestors,最近公共祖先问题),是指给定一棵有根树T,给出若干个查询LCA(u, v)(通常查询数量较大),每次求树T中两个顶点u和v的最近公共祖先,即找 ...
- [算法整理]树上求LCA算法合集
1#树上倍增 以前写的博客:http://www.cnblogs.com/yyf0309/p/5972701.html 预处理时间复杂度O(nlog2n),查询O(log2n),也不算难写. 2#st ...
- LCA算法笔记
LCA,最近公共祖先,实现有多种不同的方法,在树上的问题中有着广泛的应用,比如说树上的最短路之类. LCA的实现方法有很多,比如RMQ.树链剖分等. 今天来讲其中实现较为简单的三种算法: RMQ+时间 ...
- 对各种lca算法的理解
1.RMQ+ST 首先注意这个算法的要素:结点编号,dfs序,结点深度. 首先dfs,求出dfs序,同时求出每个结点的深度.然后st算法,维护深度最小的结点编号(dfs序也可以,因为他们俩可以互相转换 ...
随机推荐
- 【软工项目Beta阶段】第10周Scrum会议博客
第十周会议记录 小组GitHub项目地址https://github.com/ouc-softwareclass/OUC-Market 小组Issue地址https://github.com/ouc- ...
- 第六周-Scrum Meeting
第一部分ScrumMeeting 每个人的工作: 成员 任务 ISSUE链接 本周已完成的工作 本周计划完成的工作 工作中遇到的困难 李卓峻 负责商品信息页面的界面设计与功能实现 https://gi ...
- AcWing 209. 装备购买 (高斯消元线性空间)打卡
脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量z[i]=(ai,1,ai,2,..,ai,m)z[i]=(ai,1,ai,2,..,ai,m) 表示,每个装备需要 ...
- windows环境下如何安装memcached教程
Memcached 是一个开源免费高性能的分布式内存对象缓存系统,能够加快网站访问速度和减轻数据库压力,本文向大家介绍下windows环境下如何安装memcached. 工具/原料 memcach ...
- php开发面试题---Apache 运行PHP原理(整理)
php开发面试题---Apache 运行PHP原理(整理) 一.总结 一句话总结: 不要忘记 php引擎将页面静态化 和 php引擎和apache之间通讯 反思的回顾非常有用,因为决定了我的方向和技巧 ...
- Windows下 wamp下Apache配置虚拟域名
安装好wamp后 找到 找到 Include conf/extra/httpd-vhosts.conf 去掉前面的# 并保存 修改 DocumentRoot 和 ServerName ...
- how to convert from hex to disasm
cat ascii.hex | ascii2binary -b h -t us > ascii.bin x86dis -e 0 -s att -f ascii.bin echo "d8 ...
- spark textFile读取多个文件
1.spark textFile读取File 1.1 简单读取文件 val spark = SparkSession.builder() .appName("demo") .mas ...
- 关于软件IntelliJ IDEA的使用技巧(三)
二,IntelliJ IDEA的工具栏介绍 2,IntelliJ IDEA菜单栏 (9)Tools工具 ✌1.Tasks & Contexts: ✌2.Generate JavaDoc: ✌3 ...
- python基础【第十篇】
Python文件操作 1.常规格式 f = open(file="文件所在路径/文件名",mode="操作模式",encoding="选择的编码&qu ...