Ural1471-Distance in the Tree

  • 大致题意

    给定一棵树,求两个节点之间的距离。

  • 解题思路

    以0为根节点,先dfs求出遍历的节点顺序和对应的节点深度,记录每个节点在欧拉序列中第一次出现的下标,以及每个节点到根节点的距离。
    预处理RMQ能够快速得到在欧拉序列中的某个区间中离根最近的点(深度最浅),然后通过寻找两个节点u,v在欧拉序列中首次出现的位置区间内深度最浅的对应节点就是LCA(u,v),则u,v之间的距离为dis[u]-dis[t]+dis[v]-dis[t]。


  • 代码
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 50000 + 5;
const int max_logn = 18;

struct edge {
    int to, weight;
};

vector<edge> G[maxn];

int vs[maxn*2-1];           // 欧拉序列
int id[maxn];               // 节点在欧拉序列中首次出现的位置
int depth[maxn*2-1];        // 节点在欧拉序列中节点的深度
int dis[maxn];              // 节点到跟节点的距离

int st[maxn*2-1][max_logn];

int n, root;

void dfs(int v, int p, int d, int w, int &k) {
    id[v] = k;
    vs[k] = v;
    depth[k++] = d;
    dis[v] = w;
    for (int i = 0; i < G[v].size(); i++) {
        if (G[v][i].to != p) {
            dfs(G[v][i].to, v, d + 1, w + G[v][i].weight, k);
            vs[k] = v;
            depth[k++] = d;
        }
    }
}

//[l, r)
void rmq_init(int n) {
    for (int i = 0; i < n; i++) {
        st[i][0] = i;
    }
    for (int j = 1; (1 << j) < n; j++) {
        for (int i = 0; i + (1 << j) - 1 < n; i++) {
            if (depth[st[i][j-1]] <= depth[st[i+(1<<(j-1))][j-1]]) {
                st[i][j] = st[i][j-1];
            } else {
                st[i][j] = st[i+(1<<(j-1))][j-1];
            }
        }
    }
}

void init() {
    int k = 0;
    dfs(root, -1, 0, 0, k);
    rmq_init(n * 2 - 1);
}

int query(int L, int R) {
    int k = log2(R - L);
    if (depth[st[L][k]] <= depth[st[R-(1<<k)][k]]) {
        return st[L][k];
    } else {
        return st[R-(1<<k)][k];
    }
}

int lca(int u, int v) {
    return vs[query(min(id[u], id[v]), max(id[u], id[v])+1)];
}

int main(int argc, char const *argv[]) {
    scanf("%d", &n);
    root = 0;
    for (int i = 0; i < n - 1; i++) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        G[u].push_back((edge){v, w});
        G[v].push_back((edge){u, w});
    }
    init();
    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        int t = lca(u, v);
        int len = dis[u] - dis[t] + dis[v] - dis[t];
        printf("%d\n", len);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
最新发布
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值