HDU 2586——How far away ?——————【LCA模板题】
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9359 Accepted Submission(s): 3285
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.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5;
struct AdjEdge{
int to,w,next;
}adjedges[maxn];
int head[maxn];
int dis[maxn],vset[maxn],dep[maxn],d[maxn][30],first[maxn];
int tot,nn;
void init(){
tot=0;
nn=0;
memset(dep,0,sizeof(dep));
memset(head,-1,sizeof(head));
memset(dis,0,sizeof(dis));
memset(d,0,sizeof(d));
memset(first,0,sizeof(first));
}
void addedge(int _u,int _v,int _w){ //
adjedges[tot].to=_v;
adjedges[tot].w=_w;
adjedges[tot].next=head[_u];
head[_u]=tot++;
adjedges[tot].to=_u;
adjedges[tot].w=_w;
adjedges[tot].next=head[_v];
head[_v]=tot++;
} void dfs(int _u,int _fa,int _dep){
// printf("%d %d\n",_u,_dep);
dep[_u]=_dep;
vset[++nn]=_u;
first[_u]=nn;
for(int i=head[_u];i!=-1;i=adjedges[i].next){
AdjEdge & e = adjedges[i];
if(e.to!=_fa){
dis[e.to]=dis[_u]+e.w;
dfs(e.to,_u,_dep+1);
vset[++nn]=_u;
}
}
}
void ST(){
for(int i=1;i<=nn;i++)
d[i][0]=vset[i];
for(int j=1;(1<<j)<=nn;j++){
for(int i=1; i+(1<<j)-1<=nn ; i++){
if(dep[d[i][j-1]]<dep[d[i+(1<<(j-1))][j-1]])
d[i][j]=d[i][j-1];
else d[i][j]=d[i+(1 << (j-1))][j-1];
}
}
} int RMQ(int L,int R){
int k=0;
while((1<<(k+1))<=R-L+1) k++;
if(dep[d[L][k]]<=dep[d[R-(1<<k)+1][k]])
return d[L][k];
return d[R-(1<<k)+1][k];
} int main(){
int T,n,q;
scanf("%d",&T);
while(T--){
init();
int a,b,c;
scanf("%d%d",&n,&q);
for(int i=1;i<=n-1;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
dfs(1,-1,1);
ST();
for(int i=0;i<q;i++){
scanf("%d%d",&a,&b);
if(first[a]<=first[b]){
int tmp1=RMQ(first[a],first[b]);
printf("%d\n",dis[a]+dis[b]-2*dis[tmp1]);
}else{
int tmp1=RMQ(first[b],first[a]);
printf("%d\n",dis[a]+dis[b]-2*dis[tmp1]);
}
}
}
return 0;
}
HDU 2586——How far away ?——————【LCA模板题】的更多相关文章
- 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 ...
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- HDU 2544 最短路 【Dijkstra模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
- HDU 1874 畅通工程续(模板题——Floyd算法)
题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...
- HDU 1301-Jungle Roads【Kruscal】模板题
题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...
- 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)
思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...
随机推荐
- 3 K8s安裝ELK+filebeat
1 Filebeat: apiVersion: v1 kind: Service metadata: name: XX spec: ports: - name: http port: targetPo ...
- Spring Boot 专栏
http://blog.csdn.net/column/details/spring-boot.html?&page=2
- 学习Web前端的好网站推荐
说明:将其他技术技术网址也搜藏到下面,与君共同进步 1.Jquery专题 http://kb.cnblogs.com/zt/jquery/ 2.Entity Framwork专题 http://kb. ...
- 0011_练习题d1
__author__ = 'qq593' #!/usr/bin/env python #-*- coding:utf-8 -*- #使用while循环输入1 2 3 4 5 6 8 9 10 a=1 ...
- hadoop-2.3.0-cdh5.1.0完全分布式搭建(基于centos)
先参考:<hadoop-2.3.0-cdh5.1.0伪分布安装(基于centos)> http://blog.csdn.net/jameshadoop/article/details/39 ...
- 【Hadoop】hiveserver2 不能启动端口 10000 开启服务的相关经验总结
转载来自http://blog.csdn.net/lsttoy/article/details/53490144. 这个问题困扰了我三天,各种查资料踩坑填坑的尝试,终于搞定了这个问题. 首先来品尝下喜 ...
- Entity Framework Code-First(11):Configure One-to-One
Configure One-to-Zero-or-One Relationship: Here, we will configure One-to-Zero-or-One relationship b ...
- 1.如何绕过WAF(Web应用防火墙)
一:大小写转换法: 看字面就知道是什么意思了,就是把大写的小写,小写的大写.比如: SQL:sEleCt vERsIoN(); XSS:)</script> 出现原因:在waf里,使用 ...
- api接口访问限制
1.场景描述 在日常开发接口的时候,尤其是restfull接口,肯定会考虑安全或者是做一些自定义的限制,用来界定并维护代码.那么,我们都会采用什么方法那?通常来讲,我们可以通过session的形式,以 ...
- kolla-build常用命令行详解
--base-image 用于指定使用自己定制的基础镜像,不用官方网站的样例如下:kolla-build --base-image registry.access.redhat.com/rhel7/r ...