HDU2586How far away ?
http://acm.hdu.edu.cn/showproblem.php?pid=2586
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13821 Accepted Submission(s):
5195
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.
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.
the answer of the query. Output a bland line after each test case.
25
100
100
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector<int> v[];
vector<int> w[];
int f[][];//f[i][j]表示i点向上2^j层的祖先
int g[][];//g[i][j]表示i点到从i向上2^j层的祖先的距离
int dep[];
int n,m;
void dfs(int pos,int pre,int depth)
{
dep[pos]=depth;
for(int i=;i<v[pos].size();i++)
{
int t=v[pos][i];
if(t==pre) continue;
f[t][]=pos;
g[t][]=w[pos][i];
dfs(t,pos,depth+);
}
}
int query(int a,int b)
{
int sum=;
if(dep[a]<dep[b]) swap(a,b);//深度较深的点
for(int i=;i>=;i--)//找到a在深度dep[b]处的祖先
{
if(dep[f[a][i]]>=dep[b])
{
sum+=g[a][i];//a到该祖先的距离
a=f[a][i];
}
}
if(a==b) return sum;//挪到相同深度后如果在同一点直接return
int x;
for(int i=;i>=;i--)//否则a和b一起往上蹦跶
{
if(f[a][i]!=f[b][i])
{
sum+=g[a][i];
a=f[a][i];
sum+=g[b][i];
b=f[b][i];
}
}
return sum+g[a][]+g[b][];//最后蹦跶到最近公共祖先的下一层,所以要再加上上一层
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
memset(dep,-,sizeof dep);//多组数据我们初始化
memset(f,,sizeof f);
memset(g,,sizeof g);
for(int i=;i<n;i++)//md
v[i].clear(),w[i].clear();
for(int i=;i<n;i++)
{
int x,y,c;
cin>>x>>y>>c;
v[x].push_back(y);
w[x].push_back(c);
v[y].push_back(x);
w[y].push_back(c);
}
int xxx=v[].size();
dfs(,,);//dfs处理出每个点的深度,以及各种... for(int i=;<<i<=n;i++)
for(int j=;j<=n;j++)
f[j][i]=f[f[j][i-]][i-],
g[j][i]=g[f[j][i-]][i-]+g[j][i-];
for(int i=;i<=m;i++)
{
int x,y;
cin>>x>>y;
if(x==y) cout<<""<<endl;
else cout<<query(x,y)<<endl;
}
}
return ;
}
HDU2586How far away ?的更多相关文章
- hdu-2586-How far away ?(离线LCA)
题意: 给定一棵树,每条边都有一定的权值,q次询问,每次询问某两点间的距离. 分析: 这样就可以用LCA来解,首先找到u, v 两点的lca,然后计算一下距离值就可以了. 这里的计算方法是,记下根结点 ...
- hdu2586How far away ?-(LCA)
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:有n个点,有n-1条线连通,求两点间的最短距离,最近公共祖先的入门题.Tarjan离线算法. #in ...
- hdu2586How far away ?(LCA LCATarjan离线)
题目链接:acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:有n个点,同n-1条带有权值的双向边相连,有m个询问,每个询问包含两个数x,y,求x与y的最短距离. ...
- HDU2586---How far away ?(lca算法)
Problem Description There are n houses in the village and some bidirectional roads connecting them. ...
- 先说IEnumerable,我们每天用的foreach你真的懂它吗?
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- 平台之大势何人能挡? 带着你的Net飞奔吧!
镇楼图: 跨平台系列: Linux基础 1.Linux基础学习 By dnt http://www.cnblogs.com/dunitian/p/4822807.html 环境配置 1.Hyper-v ...
- 谈谈一些有趣的CSS题目(十一)-- reset.css 知多少?
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- 百度推出新技术 MIP,网页加载更快,广告呢?
我们在2016年年初推出了MIP,帮助移动页面加速(原理).内测数据表明,MIP页面在1s内加载完成.现在已经有十多家网站加入MIP项目,有更多的网站正在加入中.在我们收到的反馈中,大部分都提到了广告 ...
- 哪种缓存效果高?开源一个简单的缓存组件j2cache
背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...
随机推荐
- 快速原型设计工具-Axure RP的介绍及简单使用(生产初期向客户展示设计产品的原型-也就是展示产品)
啧啧~~ 给大家介绍一款超棒的原型设计工具--美国Axure Software Solution公司旗舰产品Axure RP 这款工具通俗的说呢,就是在项目整体需求考察后对整体设计一个简要性概括!设计 ...
- html中submit和button的区别/ window.location.href 不跳转 的问题
<input type="button"> <input type="submit"> 这两个的区别 是 button 不会自动提交表 ...
- cookie、session与token
一.详述概念 1.Cookie机制 cookie机制是采用在客户端保持状态的方案(cookie的作用就是为了解决HTTP协议无状态的缺陷所作的努力).cookie的使用是由浏览器按照一定的原则在后台自 ...
- stream数据流
首先必须先要了解Buffer,Buffer是js和c++的结合体,类数组,通常与Stream一起用: 1. (1).Buffer是个类,因此可以构建成对象 (2).buf具有数组的性质 字符串转换成二 ...
- lucence.net+盘古分词
第一步: 添加盘古和lucence的dll引用 第二步: 拷贝Dict文件夹到项目 demo里面是Dictionaries 不过官方建议改成Dict 然后把所有项右击属性 改为“如果较新则复制” 第 ...
- 分享一组矢量图标–UX图标字体库
以下内容转自:http://ux.etao.com/,原文链接:http://ued.alimama.com/posts/219 科技日新月异的今天,市面上各种分辨率.各种显示精度的显示设备层出不穷, ...
- Leetcode: Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- Web页面中5种超酷的Hover效果
hover 效果能给网页增加一些动态效果,并且使得站点更具有活力.原来的做法是使用javascript来实现这些动态效果,但是随着CSS3的引入和现代浏览器 的支持,我们可以用纯粹的CSS代码来实现这 ...
- c 头文件<ctype.h>(一)
头文件<ctype.h>中声明了一些测试字符的函数. 每个函数的参数均为int类型,参数的值必须是EOF或可用unsigned char类型表示的字符,函数返回值为int类型. 如果参数c ...
- Java Timer定时器时,每次重复执行了两次任务的解决方案
web.xml监听配置 com.numenzq.mc.service.impl.TimerListener TimerListener类 public class TimerListener impl ...