[LCA Problem] hdu2586 How far away ?

LCA即:Lowest Common Ancestor,最近公共祖先,也就是一棵树中两个节点(这是最常用的,更普遍的,可以拓展到多个节点)同处于以另外某个节点为根的子树中,并且该根结点必须尽量靠近这两个节点.比如:

        1

     2    3

 4    5     6

其中,1的左孩子是2,右孩子3,2的左孩子4右孩子5,3没有左孩子右孩子是6

则:

4和3的LCA就是1;

2和3的LCA也是1;

4和5的LCA是2;

2和5的LCA是2.


这类问题的解决方法比较多.最简单的方法就是朴素的做法:先遍历整棵树,分层,那么对于询问(u,v), We can firstly goes them into the same depth, and then just go upwards until they are stand on the same node, what's their LCA.

Also, we can use binary search to speed up it, but it can only get a O(n*log(n)) algorithm.

Moreover, We can use tarjan algorithm (O(n + q), a liner time algorithm) to solve it.

That's what I am doing:

For the problem hdu2586, we can think about how to get the final result:

as it is a tree, we can set any node as the root, and once if we set the root, for each node, we can easily get the distance from the node to the root (O(n)).

Then we would get the answer: ans of query(u, v) = dis(root to node<u>) + dis(root to node<v>) - 2 * dis(root to LCA(u, v))   (WHY?)

here is my code:

(ma dan, shu ru fa tu ran jiu mei yong le...)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <memory>
#include <algorithm>
using namespace std;

const int MAX = 40007;
struct edge {
    int from;
    int to;
    int cost;
    int next;
    edge(int _from, int _to, int _cost, int _next): from(_from), to(_to), cost(_cost), next(_next) {}
};
vector<edge> G;
struct edge2 {
    int from;
    int to;
    int num;
    int next;
    edge2(int _from, int _to, int _num, int _next): from(_from), to(_to), num(_num), next(_next) {}
};
vector<edge2> G2;
bool vis[MAX];
int father[MAX];
int dis[MAX]; //dis[i]: the distance from root(node 1) to node i
int head[MAX]; //head pointer
int head2[MAX]; //the same
int LCA[MAX]; //LCA[num] -> the num follows the edge2
int n, m;

void add_edge(int a, int b, int c) {
    G.push_back(edge(a, b, c, head[a]));
    head[a] = G.size() - 1;
}

void add_edge2(int a, int b, int c) {
    G2.push_back(edge2(a, b, c, head2[a]));
    head2[a] = G2.size() - 1;
}

int find(int x) {
    return x == father[x] ? x : father[x] = find(father[x]);
}

void tarjan(int u) {
    father[u] = u;
    vis[u] = true;
    for (int j = head2[u]; ~j; j = G2[j].next) {
        if (vis[G2[j].to]) {
            LCA[G2[j].num] = find(G2[j].to);
        }
    }
    for (int j = head[u]; ~j; j = G[j].next) {
        if (!vis[G[j].to]) {
            dis[G[j].to] = dis[u] + G[j].cost;
            tarjan(G[j].to);
            father[G[j].to] = u;
        }
    }
}

inline int read() {
    char ch;
    while ((ch = getchar()) < '0' || ch > '9') {

    }
    int x = ch - '0';
    while ((ch = getchar()) >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
    }
    return x;
}

int main() {
    int T;
    T = read();
    //scanf(" %d", &T);
    while (T--) {
        n = read();
        m = read();
        //scanf(" %d %d", &n, &m);
        G.clear();
        G2.clear();
        memset(vis, false, sizeof(vis));
        memset(father, -1, sizeof(father));
        memset(head, -1, sizeof(head));
        memset(head2, -1, sizeof(head2));

        int a, b, c;
        for (int i = 1; i < n; ++i) {
            a = read();
            b = read();
            c = read();
            //scanf(" %d %d %d", &a, &b, &c);
            add_edge(a, b, c);
            add_edge(b, a, c);
        }
        for (int i = 1; i <= m; ++i) {
            a = read();
            b = read();
            //scanf(" %d %d", &a, &b);
            add_edge2(a, b, i);
            add_edge2(b, a, i);
        }
        dis[1] = 0;
        tarjan(1);
        
        for (int i = 1; i < G2.size(); i += 2) {
            edge2* e = &G2[i];
            printf("%d\n", dis[e->from] + dis[e->to] - (dis[LCA[e->num]] << 1));
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值