There are n houses in the village and some 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.

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 ;
}
												

最新文章

  1. DSP using Matlab 书内练习Example 2.1
  2. jQuery判断元素是否在可视区
  3. linux 下Time_wait过多问题解决
  4. 实时监控MySql状态
  5. Sumsets(完全背包)
  6. python匿名函数
  7. TED - How To Get Better At The Things You Care About
  8. C语言博客作业--一二维数组。
  9. 最简单的基于FFmpeg的libswscale的示例附件:测试图片生成工具
  10. 缺省源和 Vim 配置
  11. 9 月份 GitHub 上最火的 JavaScript 开源项目!
  12. Oracle数据库联机重定义讲解及错误处理
  13. 构建NTP时间服务器
  14. 网络流第一题!!!BZOJ1001
  15. React之生命周期
  16. 美国谍梦第一季/全集The Americans迅雷下载
  17. Android之Activity与fragment完整生命周期图
  18. MBProgressHUD 的类扩展方法用法
  19. 反射setAccessible()方法
  20. POJ-1160 Post Office (DP+四边形不等式优化)

热门文章

  1. [贪心,dp] Educational Codeforces Round 71 (Rated for Div. 2) C. Gas Pipeline (1207C)
  2. WeChat 搭建过程
  3. vim-0-indent(缩进)
  4. Vue2.0 -- 钩子函数/ 过度属性/常用指令/以及Vue-resoure发送请求
  5. HDU - 1166 树状数组模板(线段树也写了一遍)
  6. 用Arcgis缓存文件发布服务
  7. Web Scraping(网页抓取)基本原理 - 白话篇
  8. Java并发编程锁系列之ReentrantLock对象总结
  9. cut-trailing-bytes:二进制尾部去0小工具
  10. 类实例调用静态方法(Java)