题目链接:http://poj.org/problem?id=1986

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

Hint

Farms 2 and 6 are 20+3+13=36 apart. 

题意:

输入第1~M+1行,与POJ 1984相同,代表了农场地图。

然后再一行有一个整数K代表询问数,

再然后有K个询问u和v之间最短距离。

题解:

本题的输入确定了农场地图是一棵树,并且本题不需要知道农场之间的位置关系,所以不需要记录东西南北。

树上两点间的最短距离,有两种情况:

①u是v的祖先,则dist(u,v) = dist(root,v) - dist(root,u)

②u不是v的祖先,那么从u到v必然要经过LCA(u,v),显然就是最短路径,则dist(u,v) = dist(root,u) - dist(root,LCA(u,v)) + dist(root,v) - dist(root,LCA(u,v))

不难发现,第①种情况下,dist(root,LCA(u,v)) = dist(root,u),那么①和②就可以统一为:dist(u,v) = dist(root,u) + dist(root,v) - 2 * dist(root,LCA(u,v))

所以我们只要计算出每个节点和树根的距离,求出所有查询(u,v)的LCA(u,v),就能得到dist(u,v)。

AC代码:

#include<cstdio>
#include<vector>
using namespace std; const int maxn=+; //节点数
const int maxm=+; //边数
const int maxq=+; //查询数 int par[maxn];
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));} struct Edge{
int u,v,w;
Edge(int u=,int v=,int w=){this->u=u,this->v=v,this->w=w;}
};
vector<Edge> E;
vector<int> Ge[maxn];
void addedge(int u,int v,int w)
{
E.push_back(Edge(u,v,w));
Ge[u].push_back(E.size()-);
} struct Query{
int u,v;
int lca;
Query(int u=,int v=,int lca=){this->u=u,this->v=v,this->lca=lca;}
};
vector<Query> Q;
vector<int> Gq[maxn];
void addquery(int u,int v)
{
Q.push_back(Query(u,v));
Gq[u].push_back(Q.size()-);
} bool vis[maxn];
int dist[maxn];
void LCA(int u,int d)
{
par[u]=u; //建立以u为代表元素的集合
vis[u]=;
dist[u]=d;
for(int i=;i<Ge[u].size();i++)
{
Edge &e=E[Ge[u][i]]; int v=e.v;
if(!vis[v])
{
LCA(v,d+e.w);
par[v]=u; //将v的集合并入u的集合
}
}
for(int i=;i<Gq[u].size();i++)
{
Query &q=Q[Gq[u][i]]; int v=q.v;
if(vis[v])
{
q.lca=find(v);
Q[Gq[u][i]^].lca=q.lca;
}
}
} int m,n,k;
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v,w; char d[];
scanf("%d%d%d%s",&u,&v,&w,d);
addedge(u,v,w);
addedge(v,u,w);
} scanf("%d",&k);
for(int i=;i<=k;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addquery(u,v);
addquery(v,u);
} LCA(,); for(int i=;i<=k;i++)
{
printf("%d\n",dist[Q[(i-)*].u]+dist[Q[(i-)*].v]-*dist[Q[(i-)*].lca]);
}
}

POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]的更多相关文章

  1. POJ.1986 Distance Queries ( LCA 倍增 )

    POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...

  2. POJ 1986 Distance Queries LCA两点距离树

    标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...

  3. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  4. POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】

    任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total ...

  5. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

  6. POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】

    <题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码 ...

  7. POJ 1330 Nearest Common Ancestors (模板题)【LCA】

    <题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...

  8. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

  9. poj 1986 Distance Queries 带权lca 模版题

    Distance Queries   Description Farmer John's cows refused to run in his marathon since he chose a pa ...

随机推荐

  1. Android学习——在Android中使用OpenCV的第一个程序

    刚開始学习Android,因为之前比較熟悉OpenCV,于是就想先在Android上执行OpenCV试试 =============================================== ...

  2. Web实时通信之Socket.IO

    前面两篇文章使用了Ajax long polling和WebSocket两种常用的Web实时通信方式构建了简单的聊天程序. 但是,由于浏览器的兼容问题,不是所有的环境都可以使用WebSocket这种比 ...

  3. iOS 解决UIScrollView布局问题(布局受statusBar和NavigationBar影响)

    iOS APP中有一个非常好用的功能,那就是当我们在滚动一个UIScrollView滚动了很远很远的时候,假如我们想让UIScrollView回到顶部,我们绝大多数人的做法就是慢慢慢慢的滚动UIScr ...

  4. 查找被占用的端口的服务并kill掉

    转自:http://blog.csdn.net/gsls200808/article/details/52456136 方法: C:\>netstat -ano|findstr 8000 TCP ...

  5. 免费SVN、Git项目托管主机推荐

    Unfuddle 200MB的免费空间,界面友好,特性丰富,支持Git,但只能一个账户一个用户并且只允许一个项目,付费服务相对来说价格偏高 CodeSpaces 500MB,一个账户两个免费用户,付费 ...

  6. SpringBoot(十一)-- 动态数据源

    SpringBoot中使用动态数据源可以实现分布式中的分库技术,比如查询用户 就在用户库中查询,查询订单 就在订单库中查询. 一.配置文件application.properties # 默认数据源 ...

  7. Twitter 高并发高可用架构

    解决 Twitter的“问题”就像玩玩具一样,这是一个很有趣的扩展性比喻.每个人都觉得 Twitter很简单,一个菜鸟架构师随便摆弄一下个可伸缩的 Twitter就有了,就这么简单.然而事实不是这样, ...

  8. could not bind to address 0.0.0.0:80 no listening sockets available, shutting down

    在启动apache服务的时候(service httpd start 启动)出现这个问题. 出现这个问题,是因为APACHE的默认端口被占用的缘故.解决方法就是把这个端口占用的程序占用的端口去掉. 使 ...

  9. shell 获取脚本的绝对路径

    basepath=$(cd `dirname $0`; pwd) 在此解释下basepath : dirname $0,取得当前执行的脚本文件的父目录 cd `dirname $0`,进入这个目录(切 ...

  10. 【linux系列】cenos7安装jdk

    一.下载jdk,我采用的jdk.tar.gz的安装包 二.创建jdk安装目录 三.安装包解压 四.jdk环境变量配置 五.使配置的环境变量生效 六.检查jdk是否安装成功