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. 经典教程|10 分钟速成 Python3

    Python 是由吉多·范罗苏姆(Guido Van Rossum)在 90 年代早期设计. 它是如今最常用的编程语言之一.它的语法简洁且优美,几乎就是可执行的伪代码. 注意:这篇教程是基于 Pyth ...

  2. 《More Effective C++ 》读书笔记(二)Exception 异常

    这事篇读书笔记,只记录自己的理解和总结,一般情况不对其举例子具体说明,因为那正是书本身做的事情,我的笔记作为梳理和复习之用,划重点.我推荐学C++的人都好好读一遍Effective C++ 系列,真是 ...

  3. CentOS7使用阿里源安装最新版Docker

    卸载已经安装的Docker sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker ...

  4. Node2vec 代码分析

    Node2vec 代码从Github上clone到本地,主要是main.py和node2vec.py两个文件. 下面把我的读代码注释放到上面来, import numpy as np import n ...

  5. Redis 指令

    一个key可以存放将近40亿条数据 选择库    select 2  (代表选择第三个库) 增加key  set db_number  11 删除key  del key 获取值   get db_n ...

  6. Amazon及其亏本诱饵策略还能坚持多久?

    Amazon 刚刚公布了最新的财报,亏损 4100 万美元.这是这家电子商务巨头连续 5 个季度以来的第 3 次亏损.但是华尔街似乎却一片叫好声,当日 Amazon 的股价也涨了近 8 个点达 359 ...

  7. 2017年度网络安全服务企业TOP50

    何谓“大安全”? 近几年来,网络安全和信息安全领域不时出现引发社会各界关注的事件. 2014年,政府采购计划对WIN8说“不”,同年,中央网络安全和信息化领导小组成立,将网络安全上升到了国家战略高度, ...

  8. 0917 词法分析程序(java版)

    1.运行结果: 2.源代码: package 词法分析;import java.util.Scanner;public class fenxi {public static void main(Str ...

  9. scrum立会报告+燃尽图(第二周第四次)

    此作业要求参考: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2249 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公 ...

  10. 从一个app开始学iOS

    在大学上了4年学,老师一直给灌输的思想就是,从细微处着手最后看到整体.举个网站的例子.第一个学期老师安排一门课java语言,期末考试就是考试java语言的知识.第二学期java web,第一次课配置j ...