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 ..+M: Same format as "Navigation Nightmare" 

* Line +M: A single integer, K.  <= K <= , 

* Lines +M..+M+K: Each line corresponds to a distance query and contains the indices of two farms. 

Output

* Lines ..K: For each distance query, output on a single line an integer giving the appropriate distance. 
Sample Input
   E
E
S
N
W
S

Sample Output


Hint

Farms  and  are ++= apart. 

Source

 

题意:给一棵带权重的树,共有k个查询,每次查询树中2个结点的距离。结点数n最大为40000,k最大10000

分析:首先我们将无根树转为有根树,可以在O(n)时间内得到每个结点到根结点的距离。由于在树中从一个结点走到另一个结点的路径是唯一的,所以a到b的路径一定经过lca(a,b),设lca(a,b)=c。此时不难发现d(a,b)=d(a,root)+d(b,root)-2*d(c,root)。

这里用的是并查集的方法查找LCA,时间1000+ms,感觉有点慢

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n,m; int tot,qsize;
int head[N],qhead[N];
int vis[N];//标记
int dis[N];//距离
int fa[N]; struct Node
{
int from;
int to;
int next;
int cost;
}edge[N<<],qe[N];
void init()
{
tot=; qsize=; memset(head,-,sizeof(head));
memset(qhead,-,sizeof(qhead));
memset(vis,,sizeof(vis));
memset(fa,,sizeof(fa));
memset(dis,,sizeof(dis));
memset(edge,,sizeof(edge));
memset(qe,,sizeof(qe));
}
void addEdge(int s,int u,int c)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].cost=c;
edge[tot].next=head[s];
head[s]=tot++;
}
void addQedge(int s,int u){
qe[qsize].from=s;
qe[qsize].to=u;
qe[qsize].next=qhead[s];
qhead[s]=qsize++;
} /////////////////////////////////////////////////////////////
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
/////////////////////////////////////////////////////////////
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
fa[u]=u;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
dis[v]=dis[u]+edge[i].cost;
tarjan(v);
fa[v]=u;
}
}
for(int i=qhead[u];i!=-;i=qe[i].next){
int v=qe[i].to;
if(vis[v]){
qe[i].cost=dis[u]+dis[v]-*dis[find(v)];
qe[i^].cost=qe[i].cost;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){
init();
int a,b,c;
char s[];
for(int i=;i<m;i++){
scanf("%d%d%d%s",&a,&b,&c,s);
addEdge(a,b,c);
addEdge(b,a,c);
}
int q;
scanf("%d",&q);
while(q--){
scanf("%d%d",&a,&b);
addQedge(a,b);
addQedge(b,a);
}
tarjan();
for(int i=;i<qsize;i+=){
printf("%d\n",qe[i].cost);
}
}
return ;
}
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 200000
#define inf 1e12
vector<pair<int,int> >edge[N];
vector<pair<int,int> >que[N];
int n,m,q;
int ans[N];
int dis[N];
int fa[N];
int vis[N];
int sum;
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void LCA(int u,int p){
fa[u]=u;
for(int i=;i<edge[u].size();i++){
int v=edge[u][i].first;
if(v==p) continue;
dis[v]=dis[u]+edge[u][i].second;
LCA(v,u);
fa[v]=u;
}
vis[u]=;
if(sum==q) return;
for(int i=;i<que[u].size();i++){
int v=que[u][i].first;
if(vis[v]){
ans[que[u][i].second]=dis[u]+dis[v]-*dis[find(v)];
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){ for(int i=;i<N;i++){
edge[i].clear();
que[i].clear();
}
sum=;
memset(vis,,sizeof(vis)); char s[];
for(int i=;i<m;i++){
int a,b,c;
scanf("%d%d%d%s",&a,&b,&c,s);
edge[a].push_back(make_pair(b,c));
edge[b].push_back(make_pair(a,c));
} scanf("%d",&q);
for(int i=;i<q;i++){
int x,y;
scanf("%d%d",&x,&y);
que[x].push_back(make_pair(y,i));
que[y].push_back(make_pair(x,i));
ans[i]=;
}
dis[]=;
LCA(,); for(int i=;i<q;i++){
printf("%d\n",ans[i]);
}
}
return ;
}

poj 1986 Distance Queries(LCA)的更多相关文章

  1. poj 1986 Distance Queries(LCA:倍增/离线)

    计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...

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

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

  3. POJ 1986 Distance Queries (Tarjan算法求最近公共祖先)

    题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too lo ...

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

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

  5. POJ - 1986 Distance Queries(离线Tarjan算法)

    1.一颗树中,给出a,b,求最近的距离.(我没考虑不联通的情况,即不是一颗树的情况) 2.用最近公共祖先来求, 记下根结点到任意一点的距离dis[],这样ans = dis[u] + dis[v] - ...

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

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

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

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

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

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

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

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

随机推荐

  1. codevs1009

    题目地址:http://codevs.cn/problem/1009/ 分析: [TAG]FLOYD,乘法原理,高精度 [构思] 求可变换数的个数,那么就是组合数学的内容,四个原理的应用: 假如能知道 ...

  2. 引用System.Runtime.Serialization.Json

    vs2012下,重新添加一次System.Runtime.Serialization的引用

  3. 学习AJAX(一)

  4. struts2面试题

    由于找了很久的工作都没有找的,只能四处收集那个面试题的.和看面试题的 还有那个记忆力也不是很好了的,而那些公司面试的时候总会有一个面试题的!   在这里分享给大家(那个本来是想上传文件的,但是找不到的 ...

  5. C#管理IIS中的站点

    原文:http://www.knowsky.com/534237.html Microsoft自Windows Vista一起发布了IIS 7.0,这个已经是去年的话题了,随后,由.NET开发的Web ...

  6. (原+转)VS2013:正在从以下位置加载符号

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5158020.html 这段时间启动调试时,vs2013加载时间很长很长...然后下面网址给出了解决方法 ...

  7. [Leetcode] Search In Rotated Sorted Array (C++)

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  8. 给Array添加删除重复元素函数

    给Array本地对象增加一个原型方法,它用于删除数组中重复的元素(可能有多个重复), 返回值是一个包含被删除的重复条目的新数组. Array.prototype.distinct = function ...

  9. Ecstore 2.0 报表显示空白

    INSERT INTO `sdb_ectools_analysis` (`id`, `service`, `interval`, `modify`) VALUES (, ), (, ), (, ); ...

  10. spl_autoload_register()

    5.3版本增加了命名空间prepend函数   <?php // function __autoload($class) {//     include 'classes/' . $class  ...