题目链接: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. Python基础教程学习笔记:第二章 列表和元组

    1.序列中元素的索引: 第一个元素索引是0,第二个是1,依次递增 最后一个元素索引是-1,倒数第二个是-2,依次递减 2.序列(Sequence)的种类: 列表(list).元组(tuple).字符串 ...

  2. 下拉刷新 上拉更多 支持ListView GridView WebView【转载】

    转载自:http://www.stay4it.com/?p=245 老贴重发,源代码放附件了,需要的下载把. 终于有新货了.昨天改了下,在ListView和GridView加了个返回到顶部的按钮,li ...

  3. MyBatis 支持的扩展点(version:3.2.7)

    从 [MyBatis 原码解析(version:3.2.7)] 中,我们得知,MyBatis去执行SQL都是通过 DefaultSqlSession 中的工具方法去执行的. 那么问题来了,MyBati ...

  4. Linux+Redis实战教程_day01_常用命令【重点】

    3.常用命令[重点] Linux命令中参数,一般都是无序的.特殊情况下除外 3.1.磁盘管理命令 ls命令:列出目录内容 参数: -a 查询所有文件和文件夹.包含隐藏的 -l 查询详细列表    ls ...

  5. css counter的使用方法

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. PostgreSQL主备切换

    备库如何激活 在PostgreSQL(HOT-Standby)如主库出现异常.备库如何激活:来替换主库工作.有下列2种方式 备库在recovery.conf文件中有个配置项trigger_file.它 ...

  7. MVC中的一些不同之处(WebForm)

    一. 路由重定向 /// <summary> /// 路由重定向 /// </summary> /// <returns></returns> publ ...

  8. Eclipse配色方案插件

    Eclipse配色方案插件 真漂亮! 最近发现了一个Eclipse配色方案插件,这回给Eclipse配色太方便了. 插件主页:http://eclipsecolorthemes.org/ 插件提供了上 ...

  9. 【.netcore基础】MVC制器Controller依赖注入

    废话少说,直接上代码 首先我们得有一个接口类和一个实现类,方便后面注入MVC里 接口类 public interface IWeatherProvider { List<WeatherForec ...

  10. linux中根目录下各个目录的作用

    /bin 二进制可执行命令.该目录下存放着普通用户的命令 /dev 系统的设备文件,即设备的驱动程序 /home 用户主目录的基点 /lost-found 这个目录平时是空的,当系统非正常关机而留下的 ...