POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
Description
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!
Input
Lines 1..1+M: Same format as "Navigation Nightmare"
Line 2+M: A single integer, K. 1 <= K <= 10,000
Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6
Sample Output
13
3
36
Http
POJ:https://vjudge.net/problem/POJ-1986
UESTC:https://vjudge.net/problem/UESTC-256
Source
最近公共祖先LCA
题目大意
在一棵边带权的树上回答若干个两点之间距离的询问
解决思路
这题当然是用floyed解啦
(以上纯属胡扯)
在树上求两点之间的距离何必用Floyed呢?我们有LCA。
那么如何用LCA解这道题呢?关于LCA倍增算法的基本思路请参照我的这篇文章
但上文讲述的是边不带权的情况,本题中,我们要做一些改变。
首先,为了方便起见,我们设根节点为1号点,那么Depth和Parent的定义都没有变,我们再加上一个Dist[u]表示u到1的距离(注意和Depth的区别,一个是深度,记录节点数,另一个是距离,记录边权和)
那么两个点之间的距离就是Dist[u]+Dist[v]-2*Dist[LCA(u,v)]啦。
简单解释一下,设u和v的最近公共祖先为p,u和v的距离首先可以是u到p的距离+v到p的距离,而u到p的距离又可以是Dist[u]-Dist[p],v也同理,所以两式相加就可以得到上面的式子了。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
class Edge
{
public:
int v,w;
};
const int maxN=210000;
const int inf=2147483647;
int n,m;
vector<Edge> E[maxN];
int Depth[maxN];
int Dist[maxN];
int Parent[maxN][23];
bool vis[maxN];
void LCA_init();
void dfs(int u);
int LCA(int a,int b);
int main()
{
cin>>n>>m;
for (int i=1;i<=m;i++)
{
int a,b,c;
char ch;
scanf("%d%d%d",&a,&b,&c);
cin>>ch;
E[a].push_back((Edge){b,c});
E[b].push_back((Edge){a,c});
}
LCA_init();
int Q;
scanf("%d",&Q);
for (int i=1;i<=Q;i++)
{
int u,v;
scanf("%d%d",&u,&v);
cout<<Dist[u]+Dist[v]-2*Dist[LCA(u,v)]<<endl;
}
return 0;
}
void LCA_init()
{
memset(Dist,0,sizeof(Dist));
memset(Parent,0,sizeof(Parent));
memset(Depth,0,sizeof(Depth));
memset(vis,0,sizeof(vis));
dfs(1);
for (int j=1;j<=20;j++)
for (int i=1;i<=n;i++)
Parent[i][j]=Parent[Parent[i][j-1]][j-1];
return;
}
void dfs(int u)
{
vis[u]=1;
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i].v;
if (vis[v]==0)
{
Depth[v]=Depth[u]+1;
Dist[v]=Dist[u]+E[u][i].w;
Parent[v][0]=u;
dfs(v);
}
}
return;
}
int LCA(int a,int b)
{
if (Depth[a]<Depth[b])
swap(a,b);
for (int i=20;i>=0;i--)
if ((Parent[a][i]!=0)&&(Depth[Parent[a][i]]>=Depth[b]))
a=Parent[a][i];
if (a==b)
return a;
for (int i=20;i>=0;i--)
if ((Parent[a][i]!=0)&&(Parent[b][i]!=0)&&(Parent[a][i]!=Parent[b][i]))
{
a=Parent[a][i];
b=Parent[b][i];
}
return Parent[a][0];
}
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)的更多相关文章
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- 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 (Tarjan算法求最近公共祖先)
题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too lo ...
- 【LCA求最近公共祖先+vector构图】Distance Queries
Distance Queries 时间限制: 1 Sec 内存限制: 128 MB 题目描述 约翰的奶牛们拒绝跑他的马拉松,因为她们悠闲的生活不能承受他选择的长长的赛道.因此他决心找一条更合理的赛道 ...
- pdo mysql错误:Cannot execute queries while other unbuffered queries are active
运行环境:PHP 5.5.30-x64,MYSQL 5.6.27 错误代码:Cannot execute queries while other unbuffered queries are act ...
- POJ 1986(LCA and RMQ)
题意:给定一棵树,求任意两点之间的距离. 思路:由于树的特殊性,所以任意两点之间的路径是唯一的.u到v的距离等于dis(u) + dis(v) - 2 * dis(lca(u, v)); 其中dis( ...
- [最近公共祖先] POJ 3728 The merchant
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4556 Accepted: 1576 Desc ...
随机推荐
- String为值类型还是引用类型
关于String为值类型还是引用类型的讨论一直没有平息,最近一直在研究性能方面的问题,今天再次将此问题进行一次明确.希望能给大家带来点帮助. 如果有错误请指出. 来看下面例子: //值类型 int a ...
- web前端面试题记录
记录了2017年5月下旬刚毕业时面试的经典面试题 布局方面 1. 响应式布局,左侧栏目固定,右侧内容随着屏幕宽度变化而变化(高频) flex布局 position布局 css3计算宽度 float布局 ...
- 细说Linux权限
目录: 归属权与访问权 chmod:访问权限设置 chown:所属权限设置 umask:权限掩码 隐藏属性 chattr:写保护.误删保护 单独限权 setfacl 一.归属和访问权限简介 1.归属( ...
- 连接Oracle数据库的时候报了“Got minus one from a read call”
(转) 出现这种问题基本上就以下几种原因,可以查一下系统日志看看:1:数据库连接满了,扩大数据库连接池2:所登录的机子IP不在sqlnet.ora内,加入后重启listerner即可3:数据库负载均衡 ...
- Ant + Jenkies +Tomcat 自动构建部署Web项目
前言:博主资历尚浅,很多东西都还在刚起步学习的阶段,这几天开发任务比较轻,就在自己window系统下,模拟部署远程服务器,利用Jenkies + Ant + Tomcat 搭建了一个自动发布部署的环境 ...
- 简化布隆过滤器——BitMap
简化布隆过滤器--BitMap 前言 前段开发项目试就发现,一部分的代码实现存在着一些性能上的隐患.但当时忙于赶进度和由于卡发中的不稳定因素,想了许多解决方案也没有机会实施.最近,正好趁个机会进行一系 ...
- WAF指纹探测及识别技术
Web应用防护系统(也称:网站应用级入侵防御系统.英文:Web Application Firewall,简称: WAF).利用国际上公认的一种说法:Web应用防火墙是通过执行一系列针对HTTP/HT ...
- hadoop伪分布式集群搭建与安装(ubuntu系统)
1:Vmware虚拟软件里面安装好Ubuntu操作系统之后使用ifconfig命令查看一下ip; 2:使用Xsheel软件远程链接自己的虚拟机,方便操作.输入自己ubuntu操作系统的账号密码之后就链 ...
- JavaScript设计模式_10_职责链模式
职责链模式的定义是:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止.职责链模式的名字非常形象,一系列可能 ...
- git常用命令,助你快速入门
git是程序员中最常用的版本控制工具,学会git的基本使用是十分重要,特别是在公司里面的协同开发,废话不多说,下面贴出常用的命令. 1.基本配置 # 既然git是多人协同的工具,别人要看到你提交代码的 ...