[ural 2119]. Tree Hull

The meaning of problems

Given a tree, the initial set point \ (S \) is empty, a point is added to each operation \ (S \) or from \ (S \) remove a point, a query tree can minimal covering \ (S \) side communication block weight and all points.
\ (n-, Q \ Leq 3E5 \) .

answer

It is like a virtual model of the tree, but unfortunately, imaginary tree the Poor, is off-line algorithm.
But taking into account the contribution of each add / drop points, increase / decrease seems not complicated, it can be done directly dfs order.
For example with the addition of sub-point. Whenever you want to add a point \ (x \) , we find a set of points \ (S \) in order to dfs \ (x \) adjacent to the two points (one after the other), is set to \ (p \) and \ (q \) .
The \ (x \) to \ (p \ leftrightarrow q \) is not on the path of other \ (S \) in the points, but we want to join contribution was \ (x \) to \ (p \ leftrightarrow q \) from this path.
Distance
\ [len [x] - len
[lca (x, p)] - len [lca (x, q)] + len [lca (p, q)] \] Note that if no \ (P \) or can not be found \ (q \) , then to special treatment.
Complexity \ (\ mathcal O ((n-Q +) \ n-log) \) .

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 5, H = 18;
int n, q, tot, times; ll ans;
int lnk[N], nxt[N << 1], son[N << 1], w[N << 1];
int id[N], dfn[N], dep[N], fa[N][H]; ll len[N];
set <int> s;
void add (int x, int y, int z) {
    nxt[++tot] = lnk[x], lnk[x] = tot, son[tot] = y, w[tot] = z;
}
void dfs (int x, int p) {
    id[dfn[x] = ++times] = x, dep[x] = dep[p] + 1, fa[x][0] = p;
    for (int i = 1; i < H; ++i) {
        fa[x][i] = fa[fa[x][i - 1]][i - 1];
    }
    for (int j = lnk[x], y; j; j = nxt[j]) {
        y = son[j];
        if (y != p) {
            len[y] = len[x] + w[j];
            dfs(y, x);
        }
    }
}
int lca (int x, int y) {
    if (dep[x] < dep[y]) {
        swap(x, y);
    }
    int dif = dep[x] - dep[y];
    for (int i = H - 1; ~i; --i) {
        if (dif >> i & 1) {
            x = fa[x][i];
        }
    }
    if (x == y) {
        return x;
    }
    for (int i = H - 1; ~i; --i) {
        if (fa[x][i] != fa[y][i]) {
            x = fa[x][i], y = fa[y][i];
        }
    }
    return fa[x][0];
}
ll calc (int x) {
    int p = *(--s.lower_bound(dfn[x]));
    int q = *s.upper_bound(dfn[x]);
    if (dfn[x] < *s.begin()) {
        return len[q] - len[x];
    } else
    if (dfn[x] > *(--s.end())) {
        return len[x] - len[p];
    } else {
        return len[x] - len[lca(x, id[p])] - len[lca(x, id[q])] + len[lca(id[p], id[q])];
    }
}
void append (int x) {
    ans += calc(x);
    s.insert(dfn[x]);
}
void remove (int x) {
    s.erase(dfn[x]);
    ans -= calc(x);
}
int main () {
    scanf("%d", &n);
    for (int i = 1, x, y, z; i < n; ++i) {
        scanf("%d%d%d", &x, &y, &z);
        add(x, y, z), add(y, x, z);
    }
    dfs(1, 0);
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        char op[5]; int x;
        scanf("%s%d", op, &x);
        if (op[0] == '+') {
            append(x);
        } else {
            remove(x);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/psimonw/p/11803074.html