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 ...
随机推荐
- 死磕 java魔法类之Unsafe解析
问题 (1)Unsafe是什么? (2)Unsafe只有CAS的功能吗? (3)Unsafe为什么是不安全的? (4)怎么使用Unsafe? 简介 本章是java并发包专题的第一章,但是第一篇写的却不 ...
- 开源一个Mac漂亮的小工具 PPRows for Mac, 在Mac上优雅的计算你写了多少行代码
开源一个Mac漂亮的小工具 PPRows for Mac, 在Mac上优雅的计算你写了多少行代码. 开源地址: https://github.com/jkpang/PPRows
- [BZOJ1192][HNOI2006]鬼谷子的钱袋 数学
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1192 大水题,把m分成二的幂次方和. #include<cstdio> #in ...
- e.Row.RowType == DataControlRowType.DataRow详解(转)
代码语句如下: protected void OnRowCreate(object sender, GridViewRowEventArgs e) { if (e.Row.RowT ...
- linux设置ssh连接时间
相信大家经常遇到SSH连接闲置一会就断开需要重新连接的痛苦,为了使SSH连接保持足够长的时间,我们可以使用如下两种设置 1.sshd服务配置: #vi /etc/ssh/sshd_config Cli ...
- 关于Qt模态框总汇
转载请注明出处:http://www.cnblogs.com/dachen408/p/7285710.html 父窗体为QMainWindow: 当子窗体为: 1.QWidget,需要设置 this- ...
- chosen-bootstrap使用技巧
1.页面加载完成后,通过js方式设置值,无法有效显示的问题. 解决:先设置值,让后在进行初始化操作. // 设置select选中值 $("#type").val(type); // ...
- 迅为工业级arm开发板i.MX6DL开发板软件硬件全开源
i.MX6是基于ARM Cortex™-A9架构的高扩展性多核系列应用处理器,促进了如高稳定性工业平板电脑.差异化智能本.前装车载中控系统和超高清电子书阅读器等新一代应用的发展.强劲的3D图形加速引擎 ...
- CREATE CAST - 定义一个用户定义的转换
SYNOPSIS CREATE CAST (sourcetype AS targettype) WITH FUNCTION funcname (argtype) [ AS ASSIGNMENT | A ...
- C#根據當前DataGridView查詢數據導出Excel
private void btnsuggestinfo_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.C ...