`
Simone_chou
  • 浏览: 195709 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Design the city(LCA + RMQ)

    博客分类:
  • ZOJ
 
阅读更多
Design the city
Time Limit: 1 Second      Memory Limit: 32768 KB

Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.

In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.

Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.

Input

The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.

Process to the end of file.

Output

Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.

Output a blank line between each test cases.

Sample Input

4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3

Sample Output

 

3
2

2
2

 

       题意:

       给出结点数 N ,后给出 N - 1 条边。给出 Q 个询问,每个询问给出 3 个结点,输出这三点连通的最短距离。

 

       思路:

       LCA。因为 1 -> 2 + 2 -> 3 的距离等于 1 -> 3 的距离,所以将 3 个点间两两的距离求出后加和除以 2 即为答案。用 LCA 求出两两间点距离即可。输出的格式问题,每个 case 后输出一行空行,最后一组样例不需要有空行。所以用 temp 标记输出即可。

 

       AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int VMAX = 50010;
const int EMAX = VMAX * 2;

int n, ind;
int v[EMAX], fir[VMAX], next[EMAX], w[EMAX];

int cnt;
int vs[VMAX * 2], dep[VMAX * 2], id[VMAX], dis[VMAX];
bool vis[VMAX];

int dp[VMAX * 2][25];

void init () {
    ind = cnt = 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] = cnt;
    vs[cnt] = x;
    dep[cnt++] = 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[cnt] = x;
            dep[cnt++] = d;
        }
    }
}

void RMQ_init () {
    for (int i = 0; i < cnt; ++i) dp[i][0] = i;

    for (int j = 1; (1 << j) <= cnt; ++j) {
        for (int i = 0; i + (1 << j) < cnt; ++i) {
            int a = dp[i][j - 1];
            int b = dp[i + (1 << (j - 1))][j - 1];
            if (dep[a] < dep[b]) dp[i][j] = a;
            else dp[i][j] = b;
        }
    }

}

int RMQ (int L, int R) {
    int len = 0;
    while ((1 << (1 + len)) <= R - L + 1) ++len;
    int a = dp[L][len];
    int b = dp[R - (1 << len) + 1][len];
    if (dep[a] < dep[b]) return a;
    return b;
}

int LCA (int a, int b) {
    int L = min(id[a], id[b]);
    int R = max(id[a], id[b]);
    int node = RMQ(L, R);
    return vs[node];
}

int Distance (int a, int b, int c) {
    int ab = LCA(a, b);
    int ac = LCA(a, c);
    int bc = LCA(b, c);
    int res = 0;
    res += dis[a] + dis[b] - 2 * dis[ab];
    res += dis[a] + dis[c] - 2 * dis[ac];
    res += dis[b] + dis[c] - 2 * dis[bc];
    return res / 2;
}

int main() {

    int temp = 0;

    while (~scanf("%d", &n)) {
        if (temp) printf("\n");

        temp = 1;
        init();

        for (int i = 1; i <= n - 1; ++i) {
            int a, b, val;
            scanf("%d%d%d", &a, &b, &val);
            ++a; ++b;
            add_edge(a, b, val);
            add_edge(b, a, val);
        }

        dis[1] = 0;
        dfs(1, 0);
        RMQ_init();

        int q;
        scanf("%d", &q);
        while (q--) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            ++a, ++b, ++c;
            printf("%d\n", Distance(a, b, c));
        }

    }

    return 0;
}

 

 

分享到:
评论

相关推荐

    LCA与RMQ问题详解

    ### LCA与RMQ问题详解 #### 一、问题描述 **LCA(Least Common Ancestors)** 和 **RMQ(Range Minimum Query)** 是数据结构和算法领域中两个非常重要的概念,它们各自解决了不同的问题,但在某些情况下可以互相转换...

    算法之LCA与RMQ问题1

    在计算机科学领域,算法是解决问题的关键,而LCA(最近公共祖先)和RMQ(区间最值查询)是树形结构和数组处理中常见的两类问题。本文将详细介绍这两种算法及其高效解决方案。 首先,LCA(最近公共祖先)问题是在...

    LCA与RMQ相关题解1

    【LCA(最近公共祖先)】 最近公共祖先(Lowest ...总的来说,LCA和RMQ都是数据结构和算法中的经典问题,它们在处理树形结构和数组区间查询时有着广泛的应用。掌握这些方法,对于解决类似问题能提供高效的解决方案。

    RMQ和LCA详解

    关于RMQ和LCA的关系的知识,如何用RMQ和LCA的转换

    LCA RMQ 最小公共祖先 区间最小值

    LCA RMQ 最小公共祖先 区间最小值 LCA(Lowest Common Ancestor)和 RMQ(Range Minimum Query)是两种常见的数据结构算法问题。LCA 问题的目的是在一棵树中找到两个结点的最近公共祖先,而 RMQ 问题的目的是在一个...

    RMQ与LCA问题

    算法学习

    郭华阳《RMQ与LCA问题》

    **RMQ(Range Minimum Query)与LCA(Lowest Common Ancestor)问题**是图论与数据结构领域中的两个重要概念,广泛应用于算法设计和优化。这篇由郭华阳所著的国家队论文深入探讨了这两个问题及其解决方案。 **RMQ...

    打萎的RMQ 和 LCA

    RMQ(Range Minimum/Maximum Query,区间最值查询)和LCA(Least Common Ancestor,最近公共祖先)是数据结构和算法中的重要概念,它们在解决实际问题中有着广泛的应用。下面将详细介绍这两种算法的基本概念、经典...

    RMQ&LCA;问题

    该ppt讲了一种基于线段树的RMQ的ST算法问题和LCA算法,适合初学者用。

    lca-rmq:RMQ查找LCA的算法的实现

    如“重新审阅LCA问题”中所述,这将使用“范围最小查询”实现LCA。 已完成:天真RMQ,更快RMQ(使用nlogn稀疏表) 待办事项:执行±1 RMQ 天真的RMQ输出: (1(2(4..)(5..))(3(6..)(7..))) indexs : 0, 1, 2, 3,...

    RMQ与LCA ACM必备

    【RMQ(Range Minimal Query)】 RMQ,即范围最小值查询,是计算机科学中数据结构和算法设计的一个重要概念。它涉及到在一个数组或序列中寻找...通过学习和掌握RMQ和LCA,参赛者能够在竞赛中获得优势,提高解题能力。

    LCA问题归约成RMQ求解

    在计算机科学领域,特别是数据结构和算法分析中,"LCA问题归约成RMQ求解"是一个重要的主题。LCA代表最远公共祖先(Least Common Ancestor),而RMQ代表范围最小值查询(Range Minimum Query)。这两个概念常用于树形...

    3.郭华阳《RMQ与LCA问题》.ppt

    3.郭华阳《RMQ与LCA问题》.ppt

    ACM中的RMQ和LCA问题

    在ACM(国际大学生程序设计竞赛)中,RMQ(Range Minimal Query)和LCA(Least Common Ancestor)是两种常见的数据结构问题,通常出现在树形结构或数组中。 RMQ问题指的是在一个数组中,对给定区间进行最小值查询。...

    RMQ & LCA 问题及其相互关系

    ### RMQ与LCA问题及其相互关系 #### 引言 在算法领域,Range Minimum Query (RMQ) 和 Lowest Common Ancestor (LCA) 问题不仅因其自身的挑战性而受到关注,更因为它们在字符串处理、计算生物学以及其他复杂数据...

    RMQ以及LCA:最近公共祖先

    ### RMQ与LCA:最近公共祖先解析及解法 #### 一、引言 在计算机科学领域,尤其是在算法设计中,“最近公共祖先”(LCA, Least Common Ancestor)和“区间最小值查询”(RMQ, Range Minimum Query)是两个非常重要...

    3.郭华阳《RMQ与LCA问题》1

    **RMQ 与 LCA 问题的提出** RMQ(Range Minimum Query)问题是指在一个数组中,查询一段连续子序列的最小值。例如,给定一个数组 A[1...n],对于任意的 l 和 r (1 ≤ l ≤ r ≤ n),我们需要能够快速找到 A[l...r] ...

    算法合集之RMQ与LCA问题PPT学习教案.pptx

    RMQ(Range Minimum Query,区间最小值查询)与LCA(Lowest Common Ancestor,最近公共祖先)是两种常见的算法问题,广泛应用于数据结构、图论以及计算机科学竞赛中。 RMQ问题通常涉及到在一个给定的线性序列A中,...

    算法文档无代码RMQ&LCA问题

    算法文档无代码RMQ&LCA问题提取方式是百度网盘分享地址

    SAS+proc lca浅类别分析安装程序

    其中,PROC LCA(Latent Class Analysis)是SAS中的一个过程,用于执行潜在类别分析,这是一种统计方法,旨在从观测数据中识别出隐藏的、不可见的类别结构。PROC LCA尤其适用于处理多分类变量,它可以帮助研究人员...

Global site tag (gtag.js) - Google Analytics