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序也可以,因为他们俩可以互相转换 ...
随机推荐
- POJ 3904 (莫比乌斯反演)
Stancu likes space travels but he is a poor software developer and will never be able to buy his own ...
- vue2 的 过渡(动画)效果
1.在过渡 效果的使用中 ,key属性需要注意 : 有相同父元素的子元素必须有独特的 key.重复的 key 会造成渲染错误. 参考官方说明: https://cn.vuejs.org/ ...
- CSS 设置鼠标显示形状
CSS 设置鼠标显示形状 <style type="text/css"><!-- span {display:block;line-height:30px;mar ...
- SQLSERVER 时间日期函数,查询今天日期、昨天、一个星期、半年前的数据
今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0昨天的所有数据:select * from 表名 where D ...
- idea下远程debug配置
一. 背景: 在测试工作中,为方便发现代码中的逻辑问题,尝试使用远程debug模式,在测试过程中走查代码,不仅可以辅助测试减少与开发的沟通成本,更便于了解业务提升测试深度. 二. 配置方式: 1. 调 ...
- 观察者模式JDK源码解析
由于JDK中为了方便开发人员,已经写好了现成的观察者接口和被观察者类. 先来观察者接口: //观察者接口,每一个观察者都必须实现这个接口 public interface Observer { //这 ...
- vue - blog开发学习3
1.添加less 和less-loader支持 npm install less less-loader --save-dev 2.新建main.less,将这个样式添加到home.vue中的cont ...
- HashSet、LinkedHashSet、TreeSet的区别
HashSet:哈希表是通过使用称为散列法的机制来存储信息的,元素并没有以某种特定顺序来存放: LinkedHashSet:以元素插入的顺序来维护集合的链接表,允许以插入的顺序在集合中迭代: Tree ...
- HTML + CSS (上)
HTML 概念: 超文本标记语言. 核心: 语义. 主体结构: <!doctype html> //告诉浏览器这是什么语言 html //主体 head //头文件 body //页面主体 ...
- Tips using Manjaro
Set swappiness value The default swappiness value is set 60 as you can check it via the following co ...