HDU 4123 Bob's race (tree DP + rmq)

Source: Internet
Author: User

Link: HDU 4123 Bob's race

A town has n households, N-1 road connects two households, ensure n households Unicom, M inquiry, given n edge information, including

The number of connected residents and the length of the route. Then M queries, Q queries each time, and the longest continuous serial number must be found so that max (DIS [I])-min (DIS [I]) ≤

Q (L ≤ I ≤ r), the maximum output is r-L + 1. Dis [I] is the maximum distance that can be moved without repeating the road.

Solution: tree-like DP, through two DFS, 1st processed the longest distance and second long distance of the child node moving in each node, 2nd considered from the parent

Maintain the maximum distance of each node.

Then, rmq and processing are used to speed up the query, and the left boundary is enumerated to determine the right boundary. The single complexity is O (n ).

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 50005;int N, M, E, Q, first[maxn], jump[maxn * 2], dpMax[maxn][20], dpMin[maxn][20];struct Edge {    int u, v, w;    void set(int u, int v, int w) {        this->u = u;        this->v = v;        this->w = w;    }}ed[maxn * 2];inline void add_Edge (int u, int v, int w) {    ed[E].set(u, v, w);    jump[E] = first[u];    first[u] = E++;}int fmax[maxn], fidx[maxn], smax[maxn], sidx[maxn];inline void maintain(int u, int w, int v) {    if (w > smax[u]) {        smax[u] = w;        sidx[u] = v;    }    if (smax[u] > fmax[u]) {        swap(smax[u], fmax[u]);        swap(sidx[u], fidx[u]);    }}void dfs(int u, int pre) {    fmax[u] = fidx[u] = smax[u] = sidx[u] = 0;    for (int i = first[u]; i + 1; i = jump[i]) {        int v = ed[i].v;        if (v == pre)            continue;        dfs(v, u);        maintain(u, fmax[v] + ed[i].w, v);    }}void dfs(int u, int pre, int d) {    maintain(u, d, pre);    for (int i = first[u]; i + 1; i = jump[i]) {        int v = ed[i].v;        if (v == pre)            continue;        dfs(v, u, (v == fidx[u] ? smax[u] : fmax[u]) + ed[i].w);    }}void rmq_init() {    for (int i = 1; i <= N; i++)        dpMax[i][0] = dpMin[i][0] = fmax[i];    for (int k = 1; (1<<k) <= N; k++) {        for (int i = 1; i + (1<<k) - 1 <= N; i++) {            dpMax[i][k] = max(dpMax[i][k-1], dpMax[i+(1<<(k-1))][k-1]);            dpMin[i][k] = min(dpMin[i][k-1], dpMin[i+(1<<(k-1))][k-1]);        }    }}int rmq_query(int l, int r) {    int k = 0;    while ((1<<(k+1)) <= r - l + 1) k++;    return max(dpMax[l][k], dpMax[r-(1<<k)+1][k]) - min(dpMin[l][k], dpMin[r-(1<<k)+1][k]);}int main () {    while (scanf("%d%d", &N, &M) == 2 && N + M) {        int u, v, w;        E = 0;        memset(first, -1, sizeof(first));        for (int i = 1; i < N; i++) {            scanf("%d%d%d", &u, &v, &w);            add_Edge(u, v, w);            add_Edge(v, u, w);        }        dfs(1, 0);        dfs(1, 0, 0);        rmq_init();        while (M--) {            int ans = 0, mv = 1;            scanf("%d", &Q);            for (int i = 1; i <= N; i++) {                while (mv <= i && rmq_query(mv , i) > Q) mv++;                ans = max(ans, i - mv + 1);            }            printf("%d\n", ans);        }    }    return 0;}

HDU 4123 Bob's race (tree DP + rmq)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.