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. centos7.2部署docker-17.06.0-ce的bug:Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "process_linux.go:339: container init caused \"\"".

    现象: 操作系统:centos 7.2 kernel 3.10.0-327.el7.x86_64 mesos:1.3.0 docker:docker-17.06.0-ce 在做mesos验证时,通过m ...

  2. ubuntu10.10安装使用vnc

    原文发表于:2010-12-15转载至cu于:2012-07-21 搭安全试验的环境,在vmware上安装了ubuntu10.10(大学的时候用过,最早用的好像是6系列吧).安装好后想用远程桌面控制, ...

  3. mongodb windows 4 zip安装

    安装mongoDB目的:学习Express,顺带mongodb. 本文目的: 4.0.2的mongodb在windows7上竟然安装不了. 没办法,用压缩包手动安装吧... 安装环境:win7sp1x ...

  4. Linux 配置网络连接

    在VMware里,依次点击”编辑“ - ”虚拟网络编辑器“,如下图,我选择的是NAT模式: 在这个界面接着点"NAT设置",查看虚拟机的网关,这个网关在第三步要用.我这里的网关是1 ...

  5. 20172311-ASL测试 2018-1938872补充博客

    20172311-ASL测试 2018-1938872补充博客 课程:<程序设计与数据结构> 班级: 1723 姓名: 赵晓海 学号: 20172311 实验教师:王志强老师 测试日期:2 ...

  6. Codeforces Round #272 (Div. 2) E. Dreamoon and Strings dp

    题目链接: http://www.codeforces.com/contest/476/problem/E E. Dreamoon and Strings time limit per test 1 ...

  7. HashMap和HashTable源码分析

    HashMap HashMap是一个实现了Map接口的Hash表.提供所有Map的操作,并且允许null key和null value.HashMap几乎等同于HashTable,只不过HashMap ...

  8. 删除多余的自编译的内核、mysql连接不了的问题

    1.删除多余的自编译的内核 每次Debian发布内核更新,总是有某些内核选项跟自己的硬件不配套,要自己编译内核.编译多了,多余的内核就占用了多余的硬盘空间.我就试过因为/boot分区满了,而导致编译内 ...

  9. 这可能是目前最全的Redis高可用技术解决方案总结

    本文主要针对 Redis 常见的几种使用方式及其优缺点展开分析. 一.常见使用方式 Redis 的几种常见使用方式包括: Redis 单副本: Redis 多副本(主从): Redis Sentine ...

  10. Java List部分截取,获得指定长度子集合

    subList方法用于获取列表中指定范围的子列表,该列表支持原列表所支持的所有可选操作.返回列表中指定范围的子列表. 语法 subList(int fromIndex, int toIndex) fr ...