How far away ?

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

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

题意

给你一棵树,每次询问两点间的距离。

题解

跑一发离线lca,每次询问u,v,设c是u,v的lca,那么答案就是dis[u]+dis[v]-2*dis[c],其中dis表示从根到节点的距离。详见代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#define MAX_N 40003
using namespace std; bool vis[MAX_N]; struct edge {
public:
int to;
long long cost; edge(int t, long long c) : to(t), cost(c) { } edge() { }
}; vector<edge> G[MAX_N];
int q,n,m; struct node {
public:
int p, v; node(int pp, int vv) : p(pp), v(vv) { } node() { }
}; vector<node> Q[MAX_N];
int lca[MAX_N], ancestor[MAX_N]; int T, cas = ; int father[MAX_N];
void init() {
for (int i = ; i <= n; i++)
father[i] = i;
} int Find(int u) {
if (u == father[u])return u;
else return father[u] = Find(father[u]);
} void unionSet(int u,int v) {
int x = Find(u), y = Find(v);
if (x == y)return;
father[x] = y;
} bool Same(int u,int v) {
return Find(u) == Find(v);
} long long dis[MAX_N]; void Tarjan(int u,int p) {
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].to;
if (v == p)continue;
dis[v] = dis[u] + G[u][i].cost;
Tarjan(v, u);
unionSet(u, v);
ancestor[Find(u)] = u;
}
vis[u] = ;
for (int i = ; i < Q[u].size(); i++) {
int v = Q[u][i].v;
if (vis[v])
lca[Q[u][i].p] = ancestor[Find(v)];
}
} pair<int, int> qu[MAX_N]; int main() {
//cin.sync_with_stdio(false);
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &q);
m = n - ;
init();
memset(vis, , sizeof(vis));
memset(ancestor, , sizeof(ancestor));
memset(dis, , sizeof(dis));
memset(lca, , sizeof(lca));
for (int i = ; i <= n; i++)G[i].clear();
for (int i = ; i <= n; i++)Q[i].clear(); for (int i = ; i < m; i++) {
int u, v;
long long c;
scanf("%d%d%I64d", &u, &v, &c);
G[u].push_back(edge(v, c));
G[v].push_back(edge(u, c));
} for (int i = ; i < q; i++) {
int u, v;
scanf("%d%d", &u, &v);
qu[i] = make_pair(u, v);
Q[u].push_back(node(i, v));
Q[v].push_back(node(i, u));
}
Tarjan(, ); for (int i = ; i < q; i++)
printf("%I64d\n", dis[qu[i].first] + dis[qu[i].second] - * dis[lca[i]]);
}
return ;
}

HDU 2586 How far away ? 离线lca模板题的更多相关文章

  1. 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 ...

  2. hdu 2586 How far away? (LCA模板)

    题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhea ...

  3. 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 ...

  4. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  5. HDU 2586——How far away ?——————【LCA模板题】

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  7. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  8. HDU 4347 - The Closest M Points - [KDTree模板题]

    本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...

  9. HDU 2089 不要62(数位dp模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...

随机推荐

  1. Manjaro 添加国内源和安装搜狗输入法

    Manjaro 系统虽然比 Ubuntu 用着稳定,但有些小地方没有 Ubuntu 人性化,比如默认安装完的系统貌似没有中国的,Ubuntu 估计是用的人多,所以安装完后会根据所在地给你配置更新的源. ...

  2. POJ 1791 Parallelogram Counting(求平行四边形数量)

    Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...

  3. 关于前台jsp页面的js取值问题

    在后程序中传一个字符串到前台页面上,后台代码model.addAttribute("ccc", "cccc"); 在页面js上用下面两种方法取值 1. var ...

  4. 请求报文&响应报文

    转自黑马程序员视频教程

  5. pip 设置国内源提高速度

    临时使用: 可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple 例如:pip install -i https://pypi.tuna. ...

  6. 聊聊、Spring 第一篇

    Spring 大家都不陌生,企业应用中很流行的一个平台.从最开始的 Servlet 控制所有,到 MVC 模式的出现.从 SSH (Struts.Spring.Hibernate) 所谓的三剑客 到 ...

  7. 从几率到logisitic函数

    odds 几率,又称事件的优势比.几率和概率的关系如下: o=p1−pp=o1+o Logistic 回归模型的因变量只有 1/0 两种取值.假设在 p 个独立自变量 x1,x2,…,xp 作用下,记 ...

  8. 【java基础 16】抽象类和接口的区别

    导读:前两天闲着没事儿,看了本书,然后写了点代码,在接口里面写了默认方法实现,因为书上说这个特性是从java8开始的,我还特地给测了一下java7. 没过几天,就有一个技术分享会,刚好也是讲java8 ...

  9. 正则表达式与python中re模块

    一个网站,正则表达式入门的,很好 http://www.jb51.net/tools/zhengze.html 下面这个包含对python中re的介绍,也是很不错的http://www.w3cscho ...

  10. 【Luogu】P2764最小路径覆盖(拆点求最大匹配)

    题目链接 这个……学了一条定理 最小路径覆盖=原图总点数-对应二分图最大匹配数 这个对应二分图……是什么呢? 就是这样 这是原图 这是拆点之后对应的二分图. 然后咱们的目标就是从这张图上跑出个最大流来 ...