Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 11759   Accepted: 4157
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

题意:

n个点,m条边的树,不一定是树有可能有独立的点,但是这题查询的时候应该都是合法的。q次查询,求x,y的距离。

思路:

求出x,y的lca,然后减一减~~(具体看上篇)。

/*
* Author: sweat123
* Created Time: 2016/7/13 10:27:16
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int to;
int val;
int next;
}edge[MAXN*];
int dfn[MAXN*],dis[MAXN],ver[MAXN*],vis[MAXN],dp[MAXN*][],first[MAXN],n,pre[MAXN],ind,m,tot;
void add(int x,int y,int z){
edge[ind].to = y;
edge[ind].val = z;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt,int dep){
vis[rt] = ;
ver[++tot] = rt;
dfn[tot] = dep;
first[rt] = tot;
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t]){
dis[t] = dis[rt] + edge[i].val;
dfs(t,dep+);
ver[++tot] = rt;
dfn[tot] = dep;
}
}
}
void rmq(){
for(int i = ; i <= tot; i++){
dp[i][] = i;
}
for(int i = ; i < ; i++){
for(int j = ; j + ( << i) - <= tot; j++){
int x = dp[j][i-];
int y = dp[j + (<<(i-))][i-];
if(dfn[x] > dfn[y]){
dp[j][i] = y;
} else {
dp[j][i] = x;
}
}
}
}
int askrmq(int x,int y){
x = first[x];
y = first[y];
if(x > y)swap(x,y);
int k = (int)(log(y - x + ) * 1.0 / log(2.0));
int l = dp[x][k];
int r = dp[y-(<<k)+][k];
if(dfn[l] > dfn[r])return r;
else return l;
}
int main(){
while(~scanf("%d%d",&n,&m)){
ind = tot = ;
memset(vis,,sizeof(vis));
memset(pre,-,sizeof(pre));
for(int i = ; i <= m; i++){
int x,y,z;
char s[];
scanf("%d%d%d%s",&x,&y,&z,s);
add(x,y,z);
add(y,x,z);
}
memset(dis,,sizeof(dis));
dfs(,);
rmq();
int q;
scanf("%d",&q);
while(q--){
int x,y;
scanf("%d%d",&x,&y);
int tp = ver[askrmq(x,y)];
int ans = dis[x] - dis[tp] + dis[y] - dis[tp];
printf("%d\n",ans);
}
}
return ;
}

poj1986 LCA的更多相关文章

  1. POJ1986(LCA应用:求两结点之间距离)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 11304   Accepted: 3985 ...

  2. 最近公共祖先:LCA及其用倍增实现 +POJ1986

    Q:为什么我在有些地方看到的是最小公共祖先? A:最小公共祖先是LCA(Least Common Ancestor)的英文直译,最小公共祖先与最近公共祖先只是叫法不同. Q:什么是最近公共祖先(LCA ...

  3. POJ1986 Distance Queries (LCA)(倍增)

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

  4. POJ1986 DistanceQueries 最近公共祖先LCA 离线算法Tarjan

    这道题与之前那两道模板题不同的是,路径有了权值,而且边是双向的,root已经给出来了,就是1,(这个地方如果还按之前那样来计算入度是会出错的.数据里会出现多个root...数据地址可以在poj的dis ...

  5. poj1986带权lca

    lca求距离,带权值 的树上求lca,我是用倍增法求的,求两点之间的距离转化为到根节点之间的距离 (de了一个小时 的bug,重打居然就过了....) #include<map> #inc ...

  6. poj-1986 Distance Queries(lca+ST+dfs)

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

  7. [poj1986]Distance Queries(LCA)

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

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

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

  9. 带权值的LCA

    例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...

随机推荐

  1. iOS之2016面试题三

    1.OC内存管理机制 1).当你使用new,alloc和copy方法创建一个对象时,该对象的保留计数器值为 1.当你不再使用该对象时,你要负责向该对象发送一条release或autorelease消息 ...

  2. AFNetworking的封装

    AFNetworking的封装 特点 1.将AFNetworking 3.0封装.能够很好享受苹果开发中的面向对象开发思想 . 2.其中也提供几种方法,来请求数据.包括:GET请求/POST请求/Up ...

  3. iOS---stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead,

    旧方法 NSString *encoded = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; iOS9   ...

  4. 通知、block

    =================================== 通知 =================================== 一.通知(NSNotification) // N ...

  5. 锋友分享:国行和非国行iPhone的送修需知

    锋友 hfln0829 分享了一些关于苹果 iPhone 售后服务的注意事项以及有可能的维修方式.他表示,无论是国行还是港版送修,都必须关闭查找我的 iPhone 这个功能,如果你关闭不了,售后不会受 ...

  6. 2015年度总结--android开发

    虽然农历年才是新的一年的开始,不过关于中西文化的问题这里就不讨论了,所谓“男女平权,公说公有理,婆说婆有理;阴阳合历,你过你的年.” 看到很多朋友在发年度总结,于是想想这一年我都在干什么呢,也总结一下 ...

  7. 烂泥:haproxy与nginx、zabbix集成

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 昨天介绍了haproxy的手机匹配规则,今天再来介绍下haproxy与nginx.za ...

  8. zookeeper原理

    Zookeeper与paxos算法:http://www.riaos.com/ria/11299 Paxos算法1:http://blog.csdn.net/chen77716/article/det ...

  9. 匈牙利算法——S.B.S.

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...

  10. Caffe源码解析7:Pooling_Layer

    转载请注明出处,楼燚(yì)航的blog,http://home.cnblogs.com/louyihang-loves-baiyan/ Pooling 层一般在网络中是跟在Conv卷积层之后,做采样 ...