How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
思路:在lca的基础上加dis(距离根的距离)数组,在dfs处理好,ans=dis[a]-2*dis[LCA(a,b)]+dis[b];
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
#define inf 0xfffffff
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
#define maxn 40010
#define M 22
struct is
{
int v,next,w;
} edge[maxn*];
int deep[maxn],jiedge;
int dis[maxn];
int head[maxn];
int rudu[maxn];
int fa[maxn][M];
void add(int u,int v,int w)
{
jiedge++;
edge[jiedge].v=v;
edge[jiedge].w=w;
edge[jiedge].next=head[u];
head[u]=jiedge;
}
void dfs(int u)
{
for(int i=head[u]; i; i=edge[i].next)
{
int v=edge[i].v;
int w=edge[i].w;
if(!deep[v])
{
dis[v]=dis[u]+edge[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(head,,sizeof(head));
memset(fa,,sizeof(fa));
memset(rudu,,sizeof(rudu));
memset(deep,,sizeof(deep));
jiedge=;
}
int main()
{
int x,n;
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&x);
for(int i=; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
rudu[v]++;
}
for(int i=;i<=n;i++)
{
if(!rudu[i])
{
deep[i]=;
dis[i]=;
dfs(i);
break;
}
}
st(n);
while(x--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",dis[a]-*dis[LCA(a,b)]+dis[b]);
}
}
return ;
}

hdu 2586 How far away ? 带权lca的更多相关文章

  1. hdu 2874 Connections between cities 带权lca判是否联通

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)

    3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...

  3. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  4. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  5. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

  6. poj1986带权lca

    lca求距离,带权值 的树上求lca,我是用倍增法求的,求两点之间的距离转化为到根节点之间的距离 (de了一个小时 的bug,重打居然就过了....) #include<map> #inc ...

  7. hdu 3074 Zjnu Stadium (带权并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU 4359 Easy Tree DP? 带权二叉树的构造方法 dp

    题意: 给定n deep 1.构造一个n个节点的带权树,且最大深度为deep,每一个节点最多仅仅能有2个儿子 2.每一个节点的值为2^0, 2^1 ··· 2^(n-1)  随意两个节点值不能同样 3 ...

  9. HDU 2586 How far away ?【LCA模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...

随机推荐

  1. [py]可迭代对象-求最值

    for .. in ..方式遍历可迭代对象 而非下标 ## 判断是否可迭代 from collections import Iterable print(isinstance(123,Iterable ...

  2. windows server r2 之如何设置共享文件夹访问不需要输入用户名和密码

    第一步: 打开guest账号.单击桌面“开始”按钮,找到“控制面板”并打开,选择“用户帐户”并单击就会弹出一个窗口,继续单击下方的“管理其他帐户”,然后选择“Guest”,点击“启用”. 第二步: 在 ...

  3. EXTJS4扩展实例:如何使用filter查询treepanel

    我们在使用普通的store时,extjs提供了filterBy,filter等多种方法来过滤数据达到查询效果,但在treepanel中的streeStore却没有实现这个查询,于是,就有了这篇文章. ...

  4. .net MVC 下拉多级联动及编辑

    多级联动实现,附源码.当前,部分代码是参与博客园其它网友. 新增,前台代码: <script src="~/Scripts/jquery-1.10.2.js">< ...

  5. OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解 (转)

    cv::Matdepth/dims/channels/step/data/elemSizeThe class Mat represents an n-dimensional dense numeric ...

  6. IO—代码—基础及其用例

    字节流:文件.图片.歌曲 使用字节流的应用场景:如果是读写的数据都不需要转换成字符的时候,则使用字节流. 字节流处理单元为1个字节, 操作字节和字节数组.不能直接处理Unicode字符 字节流可用于任 ...

  7. 《Convolutional Neural Networks for Sentence Classification》 文本分类

    文本分类任务中可以利用CNN来提取句子中类似 n-gram 的关键信息. TextCNN的详细过程原理图见下: keras 代码: def convs_block(data, convs=[3, 3, ...

  8. SQL 中【NULL】和【无】烦躁的问题

    很烦躁,烦躁的很,总结一下. 先简单的说下: NULL   : 不确定的东西 无       :没有东西 复杂的见下文....... 一 .null值 下面举个最简单的例子,平常工作当中肯定比这个sq ...

  9. 怎么查看是否安装Scrapy

    1.在python shell 下输入 import scrapy

  10. python 用正则表达式把”0102030405”分成5组('0', '1'), ('0', '2'), ('0', '3'), ('0', '4'), ('0', '5')

    把”0102030405”分成5组('0', '1'), ('0', '2'), ('0', '3'), ('0', '4'), ('0', '5') re.findall(r"(\d)(\ ...