poj 1986 Distance Queries LCA
题目链接:http://poj.org/problem?id=1986
Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!
题目描述:已知n(n<=40000)个农场和一些农场之间的距离,给出很多组询问,在线询问每两个农场之间的距离。
算法分析:这道题运用LCA的在线和离线算法都可以。
离线LCA思路:以某一个农场为树根,用数组保存剩余每个农场到树根的距离为dist[u],那么农场u和v的距离=dist[u]+dist[v]-2*dist[q](q为LCA(u,v))。
我的做法虽然可以AC,但是很慢,有很大的优化空间:在两个节点不断向树根靠近的同时,把每向上走一层的路径上的权值(即农场之间的距离)加起来即为答案。我用离线的LCA代码运用在在线方法上,仔细想想在求每个节点的深度时就可以把离线思想中dist数组求出来了,更简单,速度更快。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int max_log_maxn=; int n,m,root;
vector<pair<int,int> > G[maxn];
int father[max_log_maxn][maxn],d[maxn];
int sum[max_log_maxn][maxn]; void dfs(int u,int p,int depth)
{
father[][u]=p;
d[u]=depth;
int num=G[u].size();
for (int i= ;i<num ;i++)
{
int v=G[u][i].first;
int len=G[u][i].second;
if (v != p)
{
sum[][v]=len;
dfs(v,u,depth+);
}
}
} void init()
{
dfs(root,-,);
for (int k= ;k+<max_log_maxn ;k++)
{
for (int i= ;i<=n ;i++)
{
if (father[k][i]<)
{
father[k+][i]=-;
sum[k+][i]=;
}
else
{
father[k+][i]=father[k][father[k][i] ];
sum[k+][i]=sum[k][i]+sum[k][father[k][i] ];
}
}
}
} int LCA(int u,int v)
{
if (d[u]>d[v]) swap(u,v);
int Sum=;
for (int k= ;k<max_log_maxn ;k++)
{
if ((d[v]-d[u])>>k & )
{
Sum += sum[k][v];
v=father[k][v];
}
}
if (u==v) return Sum;
for (int k=max_log_maxn- ;k>= ;k--)
{
if (father[k][u] != father[k][v])
{
Sum += sum[k][u];
Sum += sum[k][v];
u=father[k][u];
v=father[k][v];
}
}
Sum += sum[][u];
Sum += sum[][v];
return Sum;
} int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
int u,v,length;
char ch[];
for (int i= ;i<=n ;i++) G[i].clear();
for (int i= ;i<max_log_maxn ;i++)
{
for (int j= ;j<maxn ;j++)
sum[i][j]=;
}
for (int i= ;i<m ;i++)
{
scanf("%d%d%d%s",&u,&v,&length,ch);
G[u].push_back(make_pair(v,length));
G[v].push_back(make_pair(u,length));
}
root=;
init();
int k;
scanf("%d",&k);
for (int i= ;i<k ;i++)
{
scanf("%d%d",&u,&v);
printf("%d\n",LCA(u,v));
}
}
return ;
}
poj 1986 Distance Queries LCA的更多相关文章
- POJ.1986 Distance Queries ( LCA 倍增 )
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...
- POJ 1986 Distance Queries LCA两点距离树
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...
- POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...
- POJ 1986 Distance Queries(LCA Tarjan法)
Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- poj 1986 Distance Queries(LCA)
Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...
- poj 1986 Distance Queries 带权lca 模版题
Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a pa ...
随机推荐
- Hbase的Observer
hbase提供了类似于触发器的组件observer,类似于存储过程的endpoint. hbase中的observer分别三类,MasterObserver.RegionObserver.WALObs ...
- java高级工程师必备知识
成为Java高级工程师需要掌握哪些核心点? 每 逢长假都会有很多程序员跳槽,十一.过年是跳槽黄金时刻,尤其是过年.过年的时候年终奖到手,没有了多少牵挂,年终同学同事聚会比较多,沟通的就多,各种 工作机 ...
- shopnc二次开发(一)
---恢复内容开始--- 以前没有怎么接触过shopnc,感觉界面挺漂亮的,不过后来自己需要开发一个电商系统,就顺便参考了下,感觉构架垃圾的一塌糊涂.不过平时做这个系统二次开发的业务比较多,所以简单的 ...
- Oracle与SQL Server事务处理的比较
事务处理是所有大型数据库产品的一个关键问题,各数据库厂商都在这个方面花费了很大精力,不同的事务处理方式会导致数据库性能和功能上的巨大差异.事务处理也是数据库管理员与数据库应用程序开发人员必须深刻理解的 ...
- BT9034: 仅 IE 和 Opera 支持 HTMLFrameElement 和 HTMLIFrameElement 的 document 属性
标准参考 根据 DOM-2 中的描述,HTMLFrameElement 和 HTMLIFrameElement 都没有 'document' 属性. 关于 HTMLFrameElement 对象的详细 ...
- python之ftplib库
检测ftp是否可用 #!/usr/bin/python #coding:utf-8 from ftplib import FTP def ftp_open(ip,user,passwd): try: ...
- Moses 里的参数(未完成)
老师要求看看Moses里都有什么参数,调整了参数又会对翻译结果有什么影响,先将找到的参数列出来 首先是权重: [weight] WordPenalty0= LM= Distortion0= Phras ...
- [转]Oracle_ProC编程
1.引言 由于PL/SQL不能用来开发面向普通用户的应用程序,必须借助其他语言或开发工具. 在Linux操作系统下应该用什么语言或开发工具来进行Oracle数据库应用的开发呢?本文将介绍2种方案:Pr ...
- Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- LightOJ 1317 第八次比赛 A 题
Description You probably have played the game "Throwing Balls into the Basket". It is a si ...