杭电 How far away ?
InputFirst 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.OutputFor 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
典型的带权LCA
#include<bits/stdc++.h>
using namespace std;
const int N=1E5+;
typedef long long ll;
struct stu{
int a,b;
};
vector<stu>ve[N];
ll bits[];
int step[N];
int depth[N];
int fa[N][]; void inint(){
bits[]=;
for(int i=;i<;i++) bits[i]=bits[i-]<<;
} void dfs(int x,int y){
depth[x]=depth[y]+;
for(int i=;i<ve[x].size();i++){
if(ve[x][i].a==y){
step[x]=step[y]+ve[x][i].b;
}
}
fa[x][]=y;
for(int i=;i<;i++) fa[x][i]=fa[fa[x][i-]][i-];
for(int i=;i<ve[x].size();i++){
int dx=ve[x][i].a;
if(dx!=y){
dfs(dx,x);
}
}
}
int lca(int x,int y){
if(depth[x]<depth[y]) swap(x,y);
int dif=depth[x]-depth[y];
for(int i=;i>=;i--){
if(dif>=bits[i]){
x=fa[x][i];
dif=dif-bits[i];
}
}
if(x==y) return x;
for(int i=;i>=;i--){
if(depth[x]>=bits[i]&&fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
}
int main(){
int t;
inint();
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
int x,y,z;
for(int i=;i<=n;i++){
ve[i].clear();
}
memset(depth,,sizeof(depth));
memset(fa,,sizeof(fa));
memset(step,,sizeof(step));
for(int i=;i<=n-;i++){
scanf("%d%d%d",&x,&y,&z);
ve[x].push_back({y,z});
ve[y].push_back({x,z});
}
dfs(,);
while(m--){
int x1,y1;
scanf("%d%d",&x1,&y1);
int point=lca(x1,y1);
printf("%d\n",step[x1]+step[y1]-*step[point]);
}
}
return ;
}
杭电 How far away ?的更多相关文章
- acm入门 杭电1001题 有关溢出的考虑
最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...
- 杭电acm 1002 大数模板(一)
从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”
按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...
- C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~
暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...
- 杭电ACM2076--夹角有多大(题目已修改,注意读题)
杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...
- 杭电ACM2092--整数解
杭电ACM2092--整数解 分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...
- 杭电2034——人见人爱A-B
#include <stdio.h> #include <algorithm> using namespace std; int main () { int a[110],b[ ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
随机推荐
- coding++ :在引入的css或者js文件后面加参数的作用
前沿: 有些小伙伴们在页面(F12)直接对 JS.CSS 文件进行编辑.或者打断点调试的时候 可能会发现 所有的操作都不生效,为什么? 原因可能存在以下情况 有时候可能会遇到js或者css文件引用后传 ...
- Python用户终端输入
#用户输入,操作 print("python 用户输入操作") # input(提示字符串),函数阻塞程序,并提醒用户输入字符串 instr = input("pleas ...
- 模块 序列化 json pickle shelv xml
序列化 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes. json 模块 json.dump(d,f) json.load(f ...
- [HDU1029]Ignatius and the Princess IV<桶 水题>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目大意: 多组数据,每组数据先给一个n,然后给n各数字,找出n各数字中出现了至少(n+1)/2 ...
- [洛谷1437&Codevs1257]敲砖块<恶心的dp>
题目链接:https://www.luogu.org/problem/show?pid=1437#sub http://codevs.cn/problem/1257/ 不得不说,这个题非常的恶心,在初 ...
- 分享一款一直在维护的【网络开发运维|通用调试工具】: http请求, websocket,cmd, RSA,DES, 参数签名工具,脚本批量生成工具,google动态口令,端口检测,组件注册,js混淆...
首先发下下载地址:https://files.cnblogs.com/files/taohuadaozhu/ConfigLab.Test.ex.rar 日常开发,运维,跨部门跨公司对接中. 想快速调 ...
- C++中的各种进制转换函数汇总
C++中的各种进制转换函数汇总 1.在C中,按指定进制格式输出如下: #include <iostream> #include <cstdio> using namespace ...
- java对象clone
java克隆 为什么需要克隆 我们在很多时候需要使用一个对象去记录另外一个对象的当前状态,对象中可能会有很多属性,如果我们一个一个去设置,不仅不方便,而且效率很低,我们看一个初学者可能遇到的问题 cl ...
- Nagios监控服务
Nagios监控服务 案例1:常用系统监控命令 案例2:搭建nagios监控服务器 案例3:配置文件及插件使用 案例4:监控远程主机的公有数据 案例5:监控远程主机的私有数据 1 案例1:常用系统监控 ...
- Java工程中如何读取配置文件中参数信息
Java中读取配置文件中参数: 方法一:通过JDK中Properties来实现对配置文件的读取. Properties主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很 ...