hdu2586

How far away ?

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

Total Submission(s): 4183    Accepted Submission(s): 1598

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

分析:

LCA最近公共祖先其实就是利用树形结构求两点间的最短路;一般题目说有n个点,且仅有n-1条边,则优先考虑LCA算法(离线或在线)

程序:

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define M 40009
#include"string"
#include"map"
#include"iostream"
using namespace std;
struct st
{
int u,v,next,w;
}edge[M];
int head[M],f[M],rank[M],use[M],dis[M],t;
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void dfs(int u)
{
int i;
use[u]=1;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!use[v])
{
rank[v]=rank[u]+1;
dis[v]=dis[u]+edge[i].w;
dfs(v);
}
}
}
int LCA(int u,int v)
{
if(u==v)
return u;
else if(rank[u]>rank[v])
return LCA(f[u],v);
else
return LCA(u,f[v]);
}
int main()
{
int T,i,a,b,c,m,n;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
f[i]=i;
init();
for(i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
f[b]=a;
}
memset(dis,0,sizeof(dis));
memset(rank,0,sizeof(rank));
memset(use,0,sizeof(use));
for(i=1;i<=n;i++)
if(f[i]==i)
dfs(i);
while(m--)
{
scanf("%d%d",&a,&b);
int ans=LCA(a,b);
printf("%d\n",dis[a]+dis[b]-2*dis[ans]);
}
}
return 0;
}

LCA在线算法(hdu2586)的更多相关文章

  1. LCA在线算法ST算法

    求LCA(近期公共祖先)的算法有好多,按在线和离线分为在线算法和离线算法. 离线算法有基于搜索的Tarjan算法较优,而在线算法则是基于dp的ST算法较优. 首先说一下ST算法. 这个算法是基于RMQ ...

  2. LCA在线算法详解

    LCA(最近公共祖先)的求法有多种,这里先介绍第一种:在线算法. 声明一下:下面的内容参考了http://www.cnblogs.com/scau20110726/archive/2013/05/26 ...

  3. HDU 2586 How far away ?(LCA在线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...

  4. hdu 2586 lca在线算法(朴素算法)

    #include<stdio.h> #include<string.h>//用c/c++会爆栈,用g++ac #define inf 0x3fffffff #define N ...

  5. hdu2874 LCA在线算法

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

  6. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

  7. POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)

    1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...

  8. LCA最近公共祖先 ST+RMQ在线算法

    对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决.     这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...

  9. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14902   Accept ...

随机推荐

  1. AJAX乱码解决新方法

    用过AJAX的朋友肯定知道javascript是使用UTF-8国际编码,即每个汉字用3个字节来存储,但是这就造成了用AJAX来send数据的时候出现乱码.     有一种解决办法就是使用encodeU ...

  2. 关于C++输出中文乱码的解决方案

    把页面编码转换为UTP-8的编码 1.打开G:\vs2013way\VC\vcprojectitems目录 在 file.h newc++file.cpp 中写两句话 #pragma once#pra ...

  3. poj2513(无向图判欧拉路)

    链接:id=2513">点击打开链接 题意:一堆木棍左右两端涂有颜色,同样颜色的能够连接在一起,问全部木棍是否能都连上 代码: #include <map> #includ ...

  4. CentOS查看操作系统信息(重要)

    1.查看物理CPU的个数 [root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc ...

  5. PHP删除目录及目录下所有文件或删除指定文件

    PHP删除目录及目录下所有文件或删除指定文件 <?php header("content-type:text/html;charset=utf-8"); /** * 删除目录 ...

  6. Mockito单元测试框架学习

    基本使用方法: http://zhongl.iteye.com/blog/296136 一.问题:如何将mock的类自动注入到待测类,特别是在没有setter方法的情况下. 解答: 前提:待测的ser ...

  7. 给嵌入式ARM+Linux的初学者

    http://blog.csdn.net/lucykingljj/article/details/40619671

  8. 扁平化你的Qt应用程序

    什么是扁平化 这里的扁平化指的是交互设计方面的一种风格. 扁平化是随着极简注意的风潮流行起来的,这个概念最核心的地方就是放弃一切装饰效果,诸如阴影.透视,纹理,渐变等等能做出3D效果的元素一概不用.全 ...

  9. day21<IO流+&FIle递归>

    IO流(字符流FileReader) IO流(字符流FileWriter) IO流(字符流的拷贝) IO流(什么情况下使用字符流) IO流(字符流是否可以拷贝非纯文本的文件) IO流(自定义字符数组的 ...

  10. 监控之_nrpe

    监控机上安装nagios插件和nrpe(nrpe添加为xinetd服务)   1.添加nagios用户 /usr/sbin/useradd nagios passwd nagios 2.安装nagio ...