hdu——2586 How far away ?
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17449 Accepted Submission(s): 6747
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.
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
25
100
100
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 41000
using namespace std;
int t,n,m,x,y,z,ans,tot;
int fa[N],top[N],size[N],deep[N],head[N],dis[N];
struct Edge
{
int to,dis,from,next;
}edge[N<<];
int add(int x,int y,int z)
{
tot++;
edge[tot].to=y;
edge[tot].dis=z;
edge[tot].next=head[x];
head[x]=tot;
}
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
int begin()
{
ans=;tot=;
memset(fa,,sizeof(fa));
memset(top,,sizeof(top));
memset(dis,,sizeof(dis));
memset(edge,,sizeof(edge));
memset(deep,,sizeof(deep));
memset(size,,sizeof(size));
memset(head,,sizeof(head));
}
int dfs(int x)
{
size[x]=;
deep[x]=deep[fa[x]]+;
for(int i=head[x];i;i=edge[i].next)
{
int to=edge[i].to;
if(fa[x]==to) continue;
dis[to]=dis[x]+edge[i].dis;
fa[to]=x,dfs(to);
size[x]+=size[to];
}
}
int dfs1(int x)
{
;
if(!top[x]) top[x]=x;
for(int i=head[x];i;i=edge[i].next)
{
int to=edge[i].to;
if(fa[x]!=to&&size[t]<size[to]) t=to;
}
if(t) top[t]=top[x],dfs1(t);
for(int i=head[x];i;i=edge[i].next)
{
int to=edge[i].to;
if(fa[x]!=to&&t!=to)
dfs1(to);
}
}
int lca(int x,int y)
{
for(;top[x]!=top[y];x=fa[top[x]])
if(deep[top[x]]<deep[top[y]]) swap(x,y);
if(deep[x]>deep[y]) swap(x,y);
return x;
}
int main()
{
t=read();
while(t--)
{
n=read();m=read();begin();
;i<n;i++)
{
x=read(),y=read(),z=read();
add(x,y,z),add(y,x,z);
}
dfs(),dfs1();
;i<=m;i++)
{
x=read(),y=read();
ans=dis[x]+dis[y]-*dis[lca(x,y)];
printf("%d\n",ans);
}
}
;
}
hdu——2586 How far away ?的更多相关文章
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- hdu 2586 How far away ?倍增LCA
hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...
- HDU 2586 How far away ?【LCA】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 2586.How far away ?-离线LCA(Tarjan)
2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...
- HDU 2586 How far away ? (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...
- hdu - 2586 How far away ?(最短路共同祖先问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...
- HDU 2586 How far away ?(LCA在线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...
- HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...
- HDU 2586 How far away ?【LCA模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...
- hdu 2586 How far away ?(LCA - Tarjan算法 离线 模板题)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- 微信小程序组件解读和分析:九、form表单
form表单组件说明: 表单,将组件内的用户输入的<switch/> <input/> <checkbox/> <slider/> <radio/ ...
- java 线程池第一篇 之 ThreadPoolExcutor
一:什么是线程池? java 线程池是将大量的线程集中管理的类,包括对线程的创建,资源的管理,线程生命周期的管理.当系统中存在大量的异步任务的时候就考虑使用java线程池管理所有的线程.减少系统资源的 ...
- iOS Programming Touch Events and UIResponder
iOS Programming Touch Events and UIResponder 1 Touch Events As a subclass of UIResponder, a UIView ...
- SQL转Java代码小工具
工作中使用SQL的时候很多,当使用hibernate的时候,经常遇到多行的SQL,通常在PL/SQL或其他地方写好SQL,测试没问题后,需要将SQL写到程序代码中,多行SQL需要拼接字符串,手动一行行 ...
- 迅为i.MX6UL核心板ARMCortex-A7单核NXP飞思卡尔工控行业Imx6核心板
iMX6UL核心板小巧精致,尺寸仅38mm*42mm:CPU型号iMX6UL@ 528MHz ARM Cortex-A7架构 :内存:512M DDR :存储:8G EMMC,低功耗,性能强大,性价比 ...
- c语言中的 strcpy和strncpy字符串函数使用介绍
1.strcpy函数 函数原型:char *strcpy(char *dst,char const *src) 必须保证dst字符的空间足以保存src字符,否则多余的字符仍然被复 ...
- [Python3网络爬虫开发实战] 1.9.4-Scrapyd API的安装
安装好了Scrapyd之后,我们可以直接请求它提供的API来获取当前主机的Scrapy任务运行状况.比如,某台主机的IP为192.168.1.1,则可以直接运行如下命令获取当前主机的所有Scrapy项 ...
- 运维笔记:zabbix的运用(1)安装过程
前言 如果是用了阿里云或者腾讯云,他们都有各种监控帮我们做好.但是如果是遇到了自己维护自己机房的服务器,那么一些可视化或者监控就很有意义了.监控可能有很多种方案,这里就以比较老牌通吃的zabbix来解 ...
- c++基础_矩阵乘法
#include <iostream> using namespace std; int main(){ int a,b; cin>>a>>b; long c[a] ...
- PHP:Mysqli 基础类
文章来源:http://www.cnblogs.com/hello-tl/p/7592594.html <?php /** * __construct($Mysql_config) 构造函数 $ ...