【最近公用祖先】Tree

來源:互聯網
上載者:User
A weighted tree is given. You must find the distance between two given nodes.InputThe first line contains the number of nodes of the tree
n (1 ≤ n ≤ 50000). The nodes are numbered from 0 to n – 1.Each of the next
n – 1 lines contains three integers u, v, w, which correspond to an edgewith weight
w (0 ≤ w ≤ 1000) connecting nodes u and v.The next line contains the number of queries
m (1 ≤  m ≤ 75000).In each of the next m lines there are two integers.OutputFor each query, output the distance between the nodes with the given numbers.Sample
input output
31 0 12 0 130 10 21 2
112

LCA向RMQ轉換,注意詢問時要判斷區間下界是否小於上界,並且記錄深度時不能帶權。
Accode:

#pragma comment(linker, "/STACK:0x10000000")#include <cstdio>#include <cstdlib>#include <algorithm>#include <string>#define RMQ_min(a, b) (D[a] < D[b] ? (a) : (b))const int maxN = 50010;const int maxORD = 100010;struct Edge {int v, d; Edge *next;};Edge *edge[maxN];int D[maxORD], ord[maxORD], f[maxORD][20];int fir[maxN], dist[maxN], Ind;void Dfs(int u, int Last, int Dep){    ord[++Ind] = u; fir[u] = Ind; D[Ind] = Dep;    for (Edge *p = edge[u]; p; p = p -> next)    if (p -> v - Last)    {        dist[p -> v] = dist[u] + p -> d;        Dfs(p -> v, u, Dep + 1);        ord[++Ind] = u;        D[Ind] = Dep;    }    return;}inline void RMQ_set(){    for (int i = 1; i < Ind + 1; ++i) f[i][0] = i;    for (int q = 0; 1 << q < Ind; ++q)    for (int i = 1; i + (1 << q) < Ind + 1; ++i)        f[i][q + 1] = RMQ_min(f[i][q], f[i + (1 << q)][q]);    return;}inline int RMQ(int L, int R){    if (L == R) return L;    if (L > R) std::swap(L, R); //    int q = 0; for (; 1 << q < R - L + 2; ++q); --q;    return RMQ_min(f[L][q], f[R - (1 << q) + 1][q]);}inline int getint(){    int res = 0; char tmp;    while (!isdigit(tmp = getchar()));    do res = (res << 3) + (res << 1) + tmp - '0';    while (isdigit(tmp = getchar()));    return res;}inline void Ins(int u, int v, int d){    Edge *p = new Edge; p -> v = v;    p -> d = d; p -> next = edge[u];    edge[u] = p; return;}int main(){    freopen("tree.in", "r", stdin);    freopen("tree.out", "w", stdout);    for (int n = getint(); --n;)    {        int u = getint(), v = getint(), d = getint();        Ins(u, v, d); Ins(v, u, d);    }    Dfs(0, -1, 0); RMQ_set();    for (int m = getint(); m; --m)    {        int u = getint(), v = getint();        printf("%d\n", dist[u] + dist[v] -               (dist[ord[RMQ(fir[u], fir[v])]] << 1));    }    return 0;}#undef RMQ_min

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.