Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 12950   Accepted: 4577
Case Time Limit: 1000MS

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.

Source

【分析】给你一棵树及其边权,求给定的两个点之间的距离。可用一下在线LCA的做法,找公共祖先,求距离。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 10000000000000
#define met(a,b) memset(a,b,sizeof a)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
using namespace std;
const int N = 3e6+;
const int M = 4e5+;
int n,m,k,tot=;
int fa[N][],head[N],dis[N],dep[N];
struct man{
int to,next,w;
}edg[N];
void add(int u,int v,int w){
edg[tot].to=v;edg[tot].next=head[u];edg[tot].w=w;head[u]=tot++;
}
void dfs(int u,int f){
fa[u][]=f;
for(int i=;i<;i++){
fa[u][i]=fa[fa[u][i-]][i-];
}
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(v!=f){
dis[v]=dis[u]+edg[i].w;
dep[v]=dep[u]+;
dfs(v,u);
}
}
}
int lca(int u,int v){
int U=u,V=v;
if(dep[u]<dep[v])swap(u,v);
for(int i=;i>=;i--){
if(dep[fa[u][i]]>=dep[v]){
u=fa[u][i];
}
}
if(u==v)return (abs(dis[U]-dis[V]));
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
return (dis[U]+dis[V]-*dis[fa[u][]]);
}
int main(){
met(head,-);
scanf("%d%d",&n,&m);
char str[];
int u,v,w;
while(m--){
scanf("%d%d%d",&u,&v,&w);
scanf("%s",str);
add(u,v,w);add(v,u,w);
}
dep[]=;
dfs(,);
scanf("%d",&k);
while(k--){
scanf("%d%d",&u,&v);
printf("%d\n",lca(u,v));
}
return ;
}

POJ1986 Distance Queries (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+ST+dfs)

    题目链接: Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 11531   Accepted ...

  3. [poj1986]Distance Queries(LCA)

    解题关键:LCA模板题 复杂度:$O(n\log n)$ #pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...

  4. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  5. POJ 1986 Distance Queries(LCA Tarjan法)

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

  6. 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 ...

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

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

  8. POJ 1986:Distance Queries(倍增求LCA)

    http://poj.org/problem?id=1986 题意:给出一棵n个点m条边的树,还有q个询问,求树上两点的距离. 思路:这次学了一下倍增算法求LCA.模板. dp[i][j]代表第i个点 ...

  9. poj1986 Distance Queries(lca又是一道模版题)

    题目链接:http://poj.org/problem?id=1986 题意:就是老问题求val[u]+val[v]-2*val[root]就行.还有这题没有给出不联通怎么输出那么题目给出的数据一定 ...

随机推荐

  1. RDF和Jena RDF API简介

    这是官方文章<An Introduction to RDF and the Jena RDF API>的译文.原文是在刺猬的温驯这里看到的.其中的图片没法显示了,还有一段丢失了.于是我在此 ...

  2. Theoretical comparison between the Gini Index and Information Gain criteria

    Knowledge Discovery in Databases (KDD) is an active and important research area with the promise for ...

  3. bigdecimal 保留小数位

    public class test1_format { public static void main(String[] args) { BigDecimal decimal = new BigDec ...

  4. 虚拟机下Linux读取USB设备的问题虚拟机下Linux无法读取USB设备的解决方案

    我们在虚拟机中识别USB设备有三种情况导致Linux系统不能读取到USB设备: 1. .当虚拟机的USB服务没有开启的时候 2. 若虚拟机的USB连接的设置选项没有设置好 3. Widows抢先一步, ...

  5. 初学Spring

    Spring是当今最流行的框架,今天开始,dayday同学要正式开始学习Spring了,加油 以下是一个简单的应用Spring框架的java程序 src\dayday\HelloSpring.java ...

  6. Angular【学习笔记】

    1.angular入门网站 感谢@菜鸟教程:http://www.runoob.com/angularjs/angularjs-tutorial.html 学习笔记:

  7. Log4j快速使用精简版

    Log4j快速使用精简版 1.导入log4j-1.2.17.jar包 2.在src根目录下创建log4j.properties文件 log4j.rootLogger=INFO, CONSOLE, FI ...

  8. Caliburn.Micro学习笔记目录——li-peng

    Caliburn.Micro学习笔记(一)----引导类和命名匹配规则 Caliburn.Micro学习笔记(二)----Actions Caliburn.Micro学习笔记(三)----事件聚合IE ...

  9. $("").click与onclick的区别

    onclick是绑定事件,click本身是方法作用是触发onclick事件,只要执行了元素的click()方法,下面示例 Html代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  10. spring mvc 跳转后页面cs样式表丢失

    原因:../不能正确返回 解决办法:jsp文件加<% String path = request.getContextPath(); String basePath = request.getS ...