How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14685    Accepted Submission(s): 5554

Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 
Input
First line is a single integer T(T<=10), indicating the number of test cases.
  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.
 
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 
Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

 
Sample Output
10
25
100
100
 
Source
 题意:给你一颗带权树 求任意两个结点间的最短距离
 题解:dis存结点到根的距离+lca   dis=dis[a]+dis[b]-2*dis[lca(a,b)]; 倍增法求lca
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
#define bug() printf("!!!!!!!")
using namespace std;
#define maxn 40010
#define M 22
struct node
{
int pre;
int to;
int w;
}N[maxn*];
int pre[maxn];
int deep[maxn],nedge=;
int dis[maxn];
int rudu[maxn];
int fa[maxn][M];
int t;
int f1,t1,w1;
int l,r;
int n,m;
void add(int from ,int to,int ww)
{
nedge++;
N[nedge].to=to;
N[nedge].pre=pre[from];
N[nedge].w=ww;
pre[from]=nedge;
}
void dfs(int u)
{
for(int i=pre[u];i;i=N[i].pre)
{
int v=N[i].to;
if(deep[v]==)
{
dis[v]=dis[u]+N[i].w;
deep[v]=deep[u]+;
fa[v][]=u;
dfs(v);
}
}
}
void st(int n)
{
for(int j=;j<M;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
}
int lca(int u,int v)
{
if(deep[u]<deep[v]) swap(u,v);
int d=deep[u]-deep[v];
int i;
for(i=;i<M;i++)
{
if((<<i)&d)
{
u=fa[u][i];
}
}
if(u==v) return u;
for(i=M-;i>=;i--)
{
if(fa[u][i]!=fa[v][i])
{
u=fa[u][i];
v=fa[v][i];
}
}
u=fa[u][];
return u;
}
void init()
{
memset(rudu,,sizeof(rudu));
memset(dis,,sizeof(dis));
memset(deep,,sizeof(deep));
memset(fa,,sizeof(fa));
memset(N,,sizeof(N));
memset(pre,,sizeof(pre));
nedge=;
}
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=;i<=t;i++)
{
init();
scanf("%d %d",&n,&m);
for(int j=;j<n;j++)
{
scanf("%d %d %d",&f1,&t1,&w1);
add(f1,t1,w1);
rudu[t1]++;
}
for(int j=;j<=n;j++)
{
if(rudu[j]==)
{
deep[j]=;
dis[j]=;
dfs(j);
break;
}
}
st(n);
for(int j=;j<=m;j++)
{
int aa,bb;
scanf("%d %d",&aa,&bb);
printf("%d\n",dis[aa]+dis[bb]-*dis[lca(aa,bb)]);
}
}
}
return ;
}

HDU 2586 倍增法求lca的更多相关文章

  1. 倍增法求LCA

    倍增法求LCA LCA(Least Common Ancestors)的意思是最近公共祖先,即在一棵树中,找出两节点最近的公共祖先. 倍增法是通过一个数组来实现直接找到一个节点的某个祖先,这样我们就可 ...

  2. 倍增法求lca(最近公共祖先)

    倍增法求lca(最近公共祖先) 基本上每篇博客都会有参考文章,一是弥补不足,二是这本身也是我学习过程中找到的觉得好的资料 思路: 大致上算法的思路是这样发展来的. 想到求两个结点的最小公共祖先,我们可 ...

  3. 树上倍增法求LCA

    我们找的是任意两个结点的最近公共祖先, 那么我们可以考虑这么两种种情况: 1.两结点的深度相同. 2.两结点深度不同. 第一步都要转化为情况1,这种可处理的情况. 先不考虑其他, 我们思考这么一个问题 ...

  4. 倍增法求LCA(最近公共最先)

    对于有根树T的两个结点u.v,最近公共祖先x=LCA(u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. 如图,根据定义可以看出14和15的最近公共祖先是10,   15和16的最近公共 ...

  5. 在线倍增法求LCA专题

    1.cojs 186. [USACO Oct08] 牧场旅行 ★★   输入文件:pwalk.in   输出文件:pwalk.out   简单对比时间限制:1 s   内存限制:128 MB n个被自 ...

  6. 倍增法求lca:暗的连锁

    https://loj.ac/problem/10131 #include<bits/stdc++.h> using namespace std; struct node{ int to, ...

  7. 倍增法求LCA代码加详细注释

    #include <iostream> #include <vector> #include <algorithm> #define MAXN 100 //2^MA ...

  8. 浅谈倍增法求解LCA

    Luogu P3379 最近公共祖先 原题展现 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入格式 第一行包含三个正整数 \(N,M,S\),分别表示树的结点个数.询问 ...

  9. RMQ(倍增法求ST)

    解决什么问题:区间查询最值 倍增思想:每次得出结果的范围呈2的幂次增长,有人说相当于二分,目前我觉得相当于线段树的查找. 具体理解看代码: /*倍增法求ST*/ #include<math.h& ...

随机推荐

  1. Java飞机大战MVC版

    PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...

  2. 使用Firebug或chrome-devToolBar深入学习javascript语言核心

    使用Firebug和chrome-devToolBar调试页面样式或脚本是前端开发每天必做之事.这个开发神器到底能给我们带来哪些更神奇的帮助呢?这几天看的一些资料中给了我启发,能不通过Firebug和 ...

  3. 完美的【去重留一】SQL

    DELETE consum_record FROM consum_record, ( SELECT min(id) id, user_id, monetary, consume_time FROM c ...

  4. 直线石子合并(区间DP)

    石子合并 时间限制:1000 ms  |  内存限制:65535 KB 描述有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费 ...

  5. SST:Single-Stream Temporal Action Proposals论文笔记

    SST:Single-Stream Temporal Action Proposals 这是本仙女认认真真读完且把算法全部读懂(其实也不是非常懂)的第一篇论文 CVPR2017 一作 论文写作的动机m ...

  6. PReLU——Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification

    1. 摘要 在 \(ReLU\) 的基础上作者提出了 \(PReLU\),在几乎没有增加额外参数的前提下既可以提升模型的拟合能力,又能减小过拟合风险. 针对 \(ReLU/PReLU\) 的矫正非线性 ...

  7. Tess4J -4.0.2- Linux 实践 [解决:Tess4J - Native library (linux-x86-64/libtesseract.so) not found in resource path]

    [本文编写于2018年7月5日] Tess4J是Tesseract的Java JNA wrapper.本文介绍了在CentOS 7 操作系统中使用Tess4J的步骤及注意事项.在正式开始之前,先花一点 ...

  8. spark的运行方式——转载

    本文转载自:      spark的运行方式 本文主要讲述运行spark程序的几种方式,包括:本地测试.提交到集群运行.交互式运行 等. 在以下几种执行spark程序的方式中,都请注意master的设 ...

  9. mysqldb下载地址

    mysqldb x64    https://pan.baidu.com/s/1dFJ3G0T x32及源码 https://pypi.python.org/pypi/MySQL-python/1.2 ...

  10. 原生js和jquey获取窗口宽高,滚动条,鼠标位置总结

    JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度   alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(document).height( ...