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. linux下面测试网络带宽 (转载)

    利用bmon/nload/iftop/vnstat/iptraf实时查看网络带宽状况 一.添加yum源方便安装bmon# rpm -Uhv http://apt.sw.be/redhat/el5/en ...

  2. 第17讲- UI常用组件之ImageView图片浏览

    第17讲 UI常用组件之ImageView图片浏览 二.图片浏览ImageView ImageView就是一个用来显示图片的视图: ImageView常见属性 常见属性 对应方法 说明 android ...

  3. add BOM to fix UTF-8 in Excel

    fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

  4. pyQt事件处理

    Qt事件处理01 Qt处理事件的第二种方式:"重新实现QObject::event()函数",通过重新实现event()函数,可以在事件到达特定的事件处理器之前截获并处理他们.这种 ...

  5. 判断一个key 是否在map中存在

    public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-gener ...

  6. SQLLoader3(数据文件没有分隔符时的导入)

    数据文件:D:\oracletest\ldr_tab_fiile.dat1.数据文件字段中间以制表符TAB隔开:7369 SMITH CLERK7499 ALLEN SALESMAN7521 WARD ...

  7. EXPDP和IMPDP简单测试

    一.EXPDP和IMPDP使用说明  Oracle Database 10g引入了最新的数据泵(Data Dump)技术,数据泵导出导入(EXPDP和IMPDP)的作用 1)实现逻辑备份和逻辑恢复.  ...

  8. [汇编学习笔记][第五章[BX]和loop指令]

    第五章[BX]和loop指令 前言 定义描述性符号“()”来表示一个寄存器或一个内存单元的内容,比如: (ax)表示ax中的内容,(al)表示al的内容. 约定符号ideta表示常量. 5.1 [BX ...

  9. ASP.NET母版与内容页相对路径的问题

    1. 图片问题 非常好解决 <img runat="server" src="~/images/ad468x60.gif" alt="" ...

  10. eclipse中运行tomcat找不到jre的解决办法

    一.在eclipse中选择 window--->preferences 二.runtime environment  ----->edit 三.在这个地方就可以进行选择jre了.