How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5810 Accepted Submission(s): 2182
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.
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
题意:
给出 T(0 ~ 10) 组 case,每个 case 给出 N(2 ~ 40000) 个节点和 M (1 ~ 200)个询问,后给出 N - 1 条边的两节点和权值。每次询问输出一个答案,输出 a 到 b 的距离。
思路:
LCA。RMQ 的在线算法,任何一个根为起点都可以,dfs 的时候顺便用 dis 记录到根的距离,求出 a 和 b 的 LCA c,则距离等于 dis [ a ] + dis [ b ] - 2 X dis [ c ]。因为 RMQ 数组的长度开小了,所以 WA 了很多遍。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int VMAX = 40010; const int EMAX = VMAX * 10; int n, ind; int next[EMAX], fir[VMAX], w[EMAX], v[EMAX]; int k; int dis[VMAX], vs[VMAX * 5], dep[VMAX * 5], id[VMAX]; bool vis[VMAX]; int dp[VMAX * 5][25]; void init () { ind = k = 0; memset(fir, -1, sizeof(fir)); memset(vis, 0, sizeof(vis)); } void add_edge (int f, int t, int val) { v[ind] = t; w[ind] = val; next[ind] = fir[f]; fir[f] = ind; ++ind; } void dfs (int x, int d) { id[x] = k; vs[k] = x; dep[k++] = d; vis[x] = 1; for (int e = fir[x]; e != -1; e = next[e]) { int V = v[e]; if (!vis[V]) { dis[V] = dis[x] + w[e]; dfs(V, d + 1); vs[k] = x; dep[k++] = d; } } } void RMQ_init () { for (int i = 0; i < k; ++i) dp[i][0] = i; for (int j = 1; (1 << j) <= k; ++j) { for (int i = 0; i + (1 << j) - 1 < k; ++i) { int a = dp[i][j - 1]; int b = dp[i + (1 << (j - 1))][j - 1]; if (dep[a] > dep[b]) dp[i][j] = b; else dp[i][j] = a; } } } int RMQ (int L, int R) { int len = 0; while ((1 << (len + 1)) <= (R - L + 1)) ++len; int a = dp[L][len]; int b = dp[R - (1 << len) + 1][len]; if (dep[a] > dep[b]) return b; return a; } int LCA (int a, int b) { int L = min(id[a], id[b]); int R = max(id[a], id[b]); int Min = RMQ(L, R); return vs[Min]; } int main () { int T; scanf("%d", &T); while (T--) { init(); int m; scanf("%d%d", &n, &m); for (int i = 1; i <= n - 1; ++i) { int a, b, val; scanf("%d%d%d", &a, &b, &val); add_edge(a, b, val); add_edge(b, a, val); } dfs (1, 1); RMQ_init(); while (m--) { int a, b; scanf("%d%d", &a, &b); int node = LCA(a, b); printf("%d\n", dis[a] + dis[b] - 2 * dis[node]); } } return 0; }
相关推荐
LCA(Lowest Common Ancestor)和 RMQ(Range Minimum Query)是两种常见的数据结构算法问题。LCA 问题的目的是在一棵树中找到两个结点的最近公共祖先,而 RMQ 问题的目的是在一个数组中找到两个索引之间的最小值的...
为了提高效率,可以采用在线算法如DFS+ST(深度优先搜索+稀疏表)或离线算法如Tarjan算法。DFS+ST方法通过深度优先遍历构建E数组,并计算R数组以确定访问顺序,从而在O(logn)时间内找到LCA。Tarjan算法则是通过松弛...
关于RMQ和LCA的关系的知识,如何用RMQ和LCA的转换
算法学习
在计算机科学领域,特别是数据结构和算法分析中,"LCA问题归约成RMQ求解"是一个重要的主题。LCA代表最远公共祖先(Least Common Ancestor),而RMQ代表范围最小值查询(Range Minimum Query)。这两个概念常用于树形...
**Tarjan的LCA算法详解** 最近公共祖先(Lowest Common Ancestor,简称LCA)是图论中的一个重要概念,特别是在树形结构中。在树中,两个节点的最近公共祖先指的是距离根节点最近的那个同时是这两个节点的祖先的节点...
【LCA(最近公共祖先)】 最近公共祖先(Lowest ...总的来说,LCA和RMQ都是数据结构和算法中的经典问题,它们在处理树形结构和数组区间查询时有着广泛的应用。掌握这些方法,对于解决类似问题能提供高效的解决方案。
RMQ分为在线算法和离线算法。 1. **在线算法**: 在线算法需要在接收到查询时立即给出答案。预处理阶段可能需要较长时间,但之后每次回答查询的速度非常快。例如,简单的动态规划方法虽然能实现O(1)的查询时间,但...
最小公祖问题的算法。 如“重新审阅LCA问题”中所述,这将使用“范围最小查询”实现LCA。 已完成:天真RMQ,更快RMQ(使用nlogn稀疏表) 待办事项:执行±1 RMQ 天真的RMQ输出: (1(2(4..)(5..))(3(6..)(7..)))...
RMQ(Range Minimum/Maximum Query,区间最值查询)和LCA(Least Common Ancestor,最近公共祖先)是数据结构和算法中的重要概念,它们在解决实际问题中有着广泛的应用。下面将详细介绍这两种算法的基本概念、经典...
**RMQ(Range Minimum Query)与LCA(Lowest Common Ancestor)问题**是图论与数据结构领域中的两个重要概念,广泛应用于算法设计和优化。这篇由郭华阳所著的国家队论文深入探讨了这两个问题及其解决方案。 **RMQ...
该ppt讲了一种基于线段树的RMQ的ST算法问题和LCA算法,适合初学者用。
RMQ(Range Minimum Query,区间最小值查询)与LCA(Lowest Common Ancestor,最近公共祖先)是两种常见的算法问题,广泛应用于数据结构、图论以及计算机科学竞赛中。 RMQ问题通常涉及到在一个给定的线性序列A中,...
解决LCA问题的方法主要有两类:在线算法和离线算法。在线算法需要在每次询问时快速回答,而离线算法则先一次性处理所有询问。朴素的在线算法时间复杂度较高,可达O(n),在大量询问的情况下效率较低。 本文介绍的是...
对于RMQ的求解,有两种主要的方法:在线算法和离线算法。 在线算法在预处理阶段花费较长的时间,但之后每次回答查询只需较少的时间。例如,动态规划方法虽然能直接求解,但效率较低,预处理时间复杂度为O(n^2),...
算法文档无代码RMQ&LCA问题提取方式是百度网盘分享地址
这个问题在数据结构和算法中具有广泛的应用,特别是在动态规划和在线算法的设计中。 LCA(Lowest Common Ancestor)问题则是针对树形结构的一个经典问题,它要求找出树中两个节点的最近公共祖先。最近公共祖先指的...
在算法领域,Range Minimum Query (RMQ) 和 Lowest Common Ancestor (LCA) 问题不仅因其自身的挑战性而受到关注,更因为它们在字符串处理、计算生物学以及其他复杂数据结构中的广泛应用而显得尤为重要。本文将深入...