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. python爬虫基础16-cookie在爬虫中的应用

    Cookie的Python爬虫应用 Cookie是什么 Cookie,有时也用其复数形式 Cookies,英文是饼干的意思.指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上 ...

  2. PTA 银行排队问题之单队列多窗口加VIP服务 队列+模拟

    假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选择时,假设顾客总是选择编号最小的窗口. 有些银行会给 ...

  3. POJ:2586-Y2K Accounting Bug

    Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Description Accounting for Computer Machi ...

  4. 线段树:HDU2795-Billboard(建树方式比较新奇)

    Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. BZOJ 5336: [TJOI2018]party

    状压最长公共子序列的DP数组,一维最多K(15)个数,且相邻两个数的差不超过1,2^15种状态,预处理转移 #include<cstdio> #include<algorithm&g ...

  6. HDU 4965 Fast Matrix Calculation 矩阵快速幂

    题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...

  7. 【Beta】Scrum meeting 2

    第一天:2019/6/25 前言: 第1次会议在6月日25由PM在教10-101召开. 明确所有任务要求,根据每个人的特长和项目需求分发任务,并明确项目前进方向.时长50min. 本日任务完成情况 成 ...

  8. loj2030 「SDOI2016」储能表

    ref ref 一个点就是一个数对 \((x,y)\). 记状态 \(f[i][1/0][1/0][1/0]\) 和 \(g[i][1/0][1/0][1/0]\),其中三个 \(1/0\) 取值分别 ...

  9. Leetcode33--->Search in Rotated Sorted Array(在旋转数组中找出给定的target值的位置)

    题目: 给定一个旋转数组,但是你不知道旋转位置,在旋转数组中找出给定target值出现的位置:你可以假设在数组中没有重复值出现 举例: (i.e., 0 1 2 4 5 6 7 might becom ...

  10. wordpress 获取站点的所有链接

    <?php include "wp-load.php"; $posts = new WP_Query('post_type=any&posts_per_page=-1 ...