How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22976    Accepted Submission(s): 9078

Problem Description
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.
 
Input
First 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.
 
Output
For 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
 
Source
 
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e5 + ;
int _pow[maxn], dep[maxn], dis[maxn], vis[maxn], ver[maxn];
int tot, head[maxn], dp[maxn * ][], k, first[maxn];
struct node {
int u, v, w, nxt;
} edge[maxn << ];
void init() {
tot = ;
mem(head, -);
}
void add(int u, int v, int w) {
edge[tot].v = v, edge[tot].u = u;
edge[tot].w = w, edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int DEP) {
vis[u] = ;
ver[++k] = u;
first[u] = k;
dep[k] = DEP;
for (int i = head[u]; ~i; i = edge[i].nxt) {
if (vis[edge[i].v]) continue;
int v = edge[i].v, w = edge[i].w;
dis[v] = dis[u] + w;
dfs(v, DEP + );
ver[++k] = u;
dep[k] = DEP;
}
}
void ST(int len) {
int K = (int)(log((double)len) / log(2.0));
for (int i = ; i <= len ; i++) dp[i][] = i;
for (int j = ; j <= K ; j++) {
for (int i = ; i + _pow[j] - <= len ; i++) {
int a = dp[i][j - ], b = dp[i + _pow[j - ]][j - ];
if (dep[a] < dep[b]) dp[i][j] = a;
else dp[i][j] = b;
}
}
}
int RMQ(int x, int y) {
int K = (int)(log((double)(y - x + )) / log(2.0));
int a = dp[x][K], b = dp[y - _pow[K] + ][K];
if (dep[a] < dep[b]) return a;
else return b;
}
int LCA(int u, int v) {
int x = first[u], y = first[v];
if (x > y) swap(x, y);
int ret = RMQ(x, y);
return ver[ret];
}
int main() {
for (int i = ; i < ; i++) _pow[i] = ( << i);
int t;
sf(t);
while(t--) {
int n, q;
init();
sff(n, q);
mem(vis, );
for (int i = ; i < n ; i++) {
int u, v, w;
sfff(u, v, w);
add(u, v, w);
add(v, u, w);
}
k = , dis[] = ;
dfs(, );
ST( * n - );
while(q--) {
int u, v;
sff(u, v);
int lca = LCA(u, v);
printf("%d\n", dis[u] + dis[v] - * dis[lca]);
}
}
return ;
}

How far away ? LCA求树上两点距离的更多相关文章

  1. 模板倍增LCA 求树上两点距离 hdu2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 课上给的ppt里的模板是错的,wa了一下午orz.最近总是被坑啊... 题解:树上两点距离转化为到根的距离 ...

  2. Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)

    LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...

  3. cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!

    2450. 距离 ★★   输入文件:distance.in   输出文件:distance.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 在一个村子里有N个房子,一 ...

  4. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  5. LCA - 求任意两点间的距离

    There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...

  6. HDU 2586 /// tarjan离线求树上两点的LCA

    题目大意: 询问一棵树里 u 到 v 的距离 可由 dis[ u到根 ] + dis[ v到根 ] - 2*dis[ lca(u,v) ] 得到 https://blog.csdn.net/csyzc ...

  7. Codeforces 1304E. 1-Trees and Queries 代码(LCA 树上两点距离判奇偶)

    https://codeforces.com/contest/1304/problem/E #include<bits/stdc++.h> using namespace std; typ ...

  8. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. POJ 4718 /// 树链剖分+线段树区间合并 求树上两点间的LCIS长度

    题目大意: 给定n个点 每个点都有权值 接下来给定树的n条边 第 i 个数 a[i] 表示 i+1到a[i]之间 有一条边 给定q q个询问 每次询问给出 x y 求x到y的最长上升子序列的长度 题解 ...

随机推荐

  1. Spring Cloud(五):Hystrix 监控面板【Finchley 版】

    Spring Cloud(五):Hystrix 监控面板[Finchley 版]  发表于 2018-04-16 |  更新于 2018-05-10 |  在上一篇 Hystrix 的介绍中,我们提到 ...

  2. 【MFC】VS2017新建完MFC后,没有界面,只有代码

    问题描述:双击.rc文件后提示在另一个编辑器中打开 解决方法整合: 1----- 打开工程之前先把.rc文件改个名称,然后打开工程双击解决方案管理器的.rc文件, 会显示"载入失败" ...

  3. 小米 OJ 编程比赛 02 月常规赛

    Carryon 数数字 描述 Carryon 最近迷上了数数字,然后 Starry 给了他一个区间[l,r] ,然后提了几个要求, 需要将 ll 到 rr 之间的数全部转化成 16 进制,然后连起来. ...

  4. Python3.5 Keras-Theano(含其他库)windows 安装环境

    https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-4.2.0-Windows-x86.execonda --version ...

  5. UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言

    关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...

  6. C Program基础-二维数组

    一维数组可以看作一行连续的数据,只有一个下标.C语言允许构造二维数组甚至多维数组,在实际问题中有时候常常需要用到二维数组(例如数学上的矩阵),二维数组有两个下标,以确定某个元素在数组中的位置. (一) ...

  7. 【web前端开发】浏览器兼容性处理大全

    1.居中问题 div里的内容,IE默认为居中,而FF默认为左对齐,可以尝试增加代码margin: 0 auto; 2.高度问题 两上下排列或嵌套的div,上面的div设置高度(height),如果di ...

  8. Python基础1 Hello World!

    从今天开始和大家分享一下python最基础的知识,以便帮助初学者快速入门. 最最基础的当然是hello world 了,无论哪门语言都会从它开始... 简单的‘Hello World!’ 1. 直接运 ...

  9. # ML学习小笔记—Classification

    关于本课程的相关资料http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17.html 通过模型可以分类输入,此时根据分类结果的正确与否会有一个Loss函数.找 ...

  10. xpath获取块元素下<br>下的信息

    再爬虫取字段的时候遇到一种类似下面的结构: <p> <br> "通用名称:xxxxxx" </p> 用xpath取的方式://p//text() ...