LCA在线算法(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
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.
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.
2
3 2
1 2 10
3 1 15
1 2
2 3 2 2
1 2 100
1 2
2 1
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)的更多相关文章
- LCA在线算法ST算法
求LCA(近期公共祖先)的算法有好多,按在线和离线分为在线算法和离线算法. 离线算法有基于搜索的Tarjan算法较优,而在线算法则是基于dp的ST算法较优. 首先说一下ST算法. 这个算法是基于RMQ ...
- LCA在线算法详解
LCA(最近公共祖先)的求法有多种,这里先介绍第一种:在线算法. 声明一下:下面的内容参考了http://www.cnblogs.com/scau20110726/archive/2013/05/26 ...
- HDU 2586 How far away ?(LCA在线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...
- hdu 2586 lca在线算法(朴素算法)
#include<stdio.h> #include<string.h>//用c/c++会爆栈,用g++ac #define inf 0x3fffffff #define N ...
- hdu2874 LCA在线算法
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)
1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...
- LCA最近公共祖先 ST+RMQ在线算法
对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决. 这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
随机推荐
- Windows消息目录
Windows消息目录1. WM_NULL=$0000:2. WM_CREATE=$0001: 应用程序创建一个窗口3. WM_DESTROY=$0002: 一个窗口被销毁4. WM_MOVE=$00 ...
- 利用百度地图API根据地址查询经纬度
传上来只是为了记录下三种jsonp方式,$.get(url, callback)方式不行,会出错 -- 必须指明返回类型为”json”才行. 或者使用$.getJSON()或者$.ajax({}). ...
- Asp.Net之后台载入JS和CSS
在Asp.Net开发时,用到的JS库.通用的CSS等,在很多页面都会用到,而每次都须要手动引入.相当麻烦.并且有时一旦忘了引用,还得找半天才干找到问题.那有没有什么办法可以一劳永逸的呢?答案是有的. ...
- R语言绘图布局
在R语言中,par 函数可以设置图形边距,其中oma 参数设置outer margin, mar 参数设置margin, 这些边距有什么不同呢,通过box函数可以直观的看到 box 默认在当前图形绘制 ...
- Winform appconfig修改后的更新问题
- 【Java面试题】44 java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
字节流,字符流.字节流继承于InputStream OutputStream,字符流继承于InputStreamReader OutputStreamWriter.在java.io包中还有许多其他的流 ...
- VMware Host Agent服务不能正常启动
VMware Host Agent服务不能正常启动 原因及解决方法 一直都在用VMWare Server 2.0,其他都还好,就是隔三差五的会有些小问题,比如VMware Host Agent服务不能 ...
- About {DynamicResource {x:Static SystemColors.ControlBrushKey}}
from : http://blog.sina.com.cn/s/blog_749e42850100sahi.html 前提: <system:String x:Key="{Compo ...
- brew 中的时间格式转换
char * pACNowStr = NULL; JulianType jtNow; ISHELL_GetJulianDate(pIShell, , &jtNow); pACNowStr = ...
- C# 多线程操作队列
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...