How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11699    Accepted Submission(s): 4300

Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this
village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 
Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road
connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 
Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

 
Sample Output
10
25
100
100
 
Source
ECJTU 2009 Spring Contest
一道lca入门题,题目大意是给定n户房屋,n-1条道路,询问两两房屋间的距离。我的思路是,由于是无向图,所以无固定树根,任意选节点都可。所以,先选定点1作为根,进行bfs和树上倍增。最终用距离公式dis[x]+dis[y]-2*dis[lca(x,y)]求出两房屋间的距离,其实这个公式还是蛮易懂的。不过在next这个数组名上栽了,CE滚粗。只好忍痛将next改为mnext,降低可读性。。。
17486822    2016-07-10 21:36:39    Accepted    2586    31MS    10308K    2038B    G++    ksq2013
hdu上测的,不知道为什么用宽搜比我的同学慢了两倍,是因为数组开太大吗???真心无语。。。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
bool vis[80100];
int n,bin[20],q[50100],dis[80100],deep[80100],fa[80100][20];
int u[80100],v[80100],w[80100],first[80100],mnext[80100];
void make_bin()
{
bin[0]=1;
for(int i=1;i<=16;i++)
bin[i]=bin[i-1]<<1;
}
void Init()
{
memset(vis,false,sizeof(vis));
memset(fa,0,sizeof(fa));
memset(mnext,0,sizeof(mnext));
memset(first,0,sizeof(first));
memset(q,0,sizeof(q));
memset(dis,0,sizeof(dis));
memset(deep,0,sizeof(deep));
}
void Link()
{
for(int i=1;i<=n-1;i++){
scanf("%d%d%d",&u[i],&v[i],&w[i]);
u[i+n-1]=v[i];v[i+n-1]=u[i];w[i+n-1]=w[i];
mnext[i]=first[u[i]];
mnext[i+n-1]=first[v[i]];
first[i]=i;
first[v[i]]=i+n-1;
}
}
void bfs()
{
int head=0,tail=1;
q[0]=1;vis[1]=true;
while(head^tail){
int now=q[head];head++;
for(int i=1;i<=16;i++)
if(bin[i]<=deep[now])
fa[now][i]=fa[fa[now][i-1]][i-1];
else break;
for(int i=first[now];i&&v[i]^now;i=mnext[i])
if(!vis[v[i]]){
vis[v[i]]=true;
fa[v[i]][0]=now;
deep[v[i]]=deep[now]+1;
dis[v[i]]=dis[now]+w[i];
q[tail++]=v[i];
}
}
}
int lca(int x,int y)
{
int t=deep[x]-deep[y];
for(int i=0;i<=16;i++)
if(t&bin[i])
x=fa[x][i];
for(int i=16;i>=0;i--)
if(fa[x][i]^fa[y][i])
x=fa[x][i],y=fa[y][i];
if(!(x^y))return y;
return fa[x][0];
}
int main()
{
make_bin();
int T;
scanf("%d",&T);
for(;T;T--){
Init();
int m;
scanf("%d%d",&n,&m);
Link();
bfs();
for(int x,y;m;m--){
scanf("%d%d",&x,&y);
if(deep[x]<deep[y])swap(x,y);
printf("%d\n",dis[x]+dis[y]-2*dis[lca(x,y)]);
}
}
return 0;
}

hdu 2586 How far away的更多相关文章

  1. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  2. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  3. LCA(最近公共祖先)--tarjan离线算法 hdu 2586

    HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  4. HDU 2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:求最近祖先节点的权值和 思路:LCA Tarjan算法 #include <stdio.h&g ...

  5. HDU 2586 (LCA模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 /   \ 2      3 ...

  6. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  7. 【HDU 2586 How far away?】LCA问题 Tarjan算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...

  8. hdu - 2586 How far away ?(最短路共同祖先问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...

  9. HDU 2586 How far away ?(LCA在线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...

随机推荐

  1. Problem with WinRM on Exchange 2013 Management Shell and Exchange Toolbox on a new exchange 2013 with CAFE and BE on single server installation

    While deploying MS Exchange 2013 I experienced issues with accessing the Exchange Management Shell a ...

  2. Android 沉浸式状态栏 实现方式一

    1.开源项目 https://github.com/jgilfelt/SystemBarTint

  3. 发布一个java Servlet (静态发布)

    Servlet 是sun开发的动态web资源 的技术 让 Servlet 能响应用户请求,必须将 Servlet 配置在 Web 应用中 如何将Servlet用Tomcat发布出去: 编译你的.jav ...

  4. 【Android】不依赖焦点和选中的TextView跑马灯

    前言 继承TextView,并仿照源码修改而来,主要是取消了焦点和选中了判断,也不依赖文本的宽度. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民 ...

  5. 网络&热恋NSURLConnection代理及GET¥POST请求

    1.NSURLConnection代理下载设置在本地的身骑着白马MP3 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...

  6. OC NSString(字符串)

    OC NSString(字符串) 多行文字字面量 NSString * string = @"abC" @"DEF" @"hjk" @&qu ...

  7. 软件开发与UML的关系

    今天,我们上<统一建模语言UML>.课上老师给我们讲解了软件开发与UML之间的关系:UML常用于建立软件系统的模型,适用于系统开发的不同阶段.UML的应用贯穿于系统开发的不同阶段.1.需求 ...

  8. MySQL分表自增ID解决方案

    当我们对MySQL进行分表操作后,将不能依赖MySQL的自动增量来产生唯一ID了,因为数据已经分散到多个表中. 应尽量避免使用自增IP来做为主键,为数据库分表操作带来极大的不便. 在postgreSQ ...

  9. 开篇:IT软件人员学习的书籍 - IT软件人员书籍系列文章

    读书是一件快乐的事情. 读书能够增长知识,了解社会,了解人类的思想,继而转换成智慧.无论是什么人,都需要读书,多读书,读好书,同时也要把书中的精髓记录下来,一个是当做读后感,一个是为以后如果忘记了回头 ...

  10. Session Sticky About Nginx

    Nginx以前对session 保持支持不太好,主要采用ip_hash把同一来源的客户(同一C段的IP)固定指向后端的同一台机器,ip_hash有个缺点是不能实现很好的负载均衡:直到nginx的扩展模 ...