树上差分

点差分

原理

模板题

洛谷-P3128-Max Flow Popen in new window

#include <iostream>
#include <cstring>
#define inf 0x3f3f3f3f
#define size SiZe
using namespace std;

const int N = 50010;
struct edge {
    int to, next;
};
edge e[2 * N];
int idx, head[N];
int n, k;
int w[N], d[N];
int dep[N], fa[N], size[N];
int heavy_son[N], top[N];

void add_edge (int u, int v) {
    e[idx].to = v;
    e[idx].next = head[u];
    head[u] = idx ++;
}
void dfs1 (int cur, int father) {
    size[cur] = 1;
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == father) continue;
        dep[to] = dep[cur] + 1;
        fa[to] = cur;
        dfs1(to, cur);
        size[cur] += size[to];
        if (size[to] > size[heavy_son[cur]])
            heavy_son[cur] = to;
    }
}
void dfs2 (int cur, int top_node) {
    top[cur] = top_node;
    if (heavy_son[cur]) dfs2(heavy_son[cur], top_node);
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == fa[cur] || to == heavy_son[cur]) continue;
        dfs2(to, to);
    }
}
int lca (int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    return x;
}
void dfs3 (int cur, int father) {
    w[cur] = d[cur];
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == father) continue;
        dfs3(to, cur);
        w[cur] += w[to];
    }
}

int main () {
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &k);
    for (int i = 1, u, v; i <= n - 1; ++ i) {
        scanf("%d%d", &u, &v);
        add_edge(u, v);
        add_edge(v, u);
    }

    dfs1(1, -1);
    dfs2(1, 1);
    for (int i = 1, x, y; i <= k; ++ i) {
        scanf("%d%d", &x, &y);
        ++ d[x];
        ++ d[y];
        int z = lca(x, y);
        -- d[z];
        -- d[fa[z]];
    }
    dfs3(1, -1);
    int res = -inf;
    for (int i = 1; i <= n; ++ i)
        res = max(res, w[i]);
    printf("%d", res);
    return 0;
}

边差分

原理

见《进阶指南》第380页。

模板题

AcWing-352-闇の連鎖open in new window

#include <iostream>
#include <cstring>
#define size SiZe
using namespace std;

const int N = 100010;
struct edge {
    int to, next;
};
edge e[2 * N];
int idx, head[N];
int n, m;
int w[N], d[N];
int dep[N], fa[N], size[N];
int heavy_son[N], top[N];

void add_edge (int u, int v) {
    e[idx].to = v;
    e[idx].next = head[u];
    head[u] = idx ++;
}
void dfs1 (int cur, int father) {
    size[cur] = 1;
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == father) continue;
        dep[to] = dep[cur] + 1;
        fa[to] = cur;
        dfs1(to, cur);
        size[cur] += size[to];
        if (size[to] > size[heavy_son[cur]])
            heavy_son[cur] = to;
    }
}
void dfs2 (int cur, int top_node) {
    top[cur] = top_node;
    if (heavy_son[cur]) dfs2(heavy_son[cur], top_node);
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == fa[cur] || to == heavy_son[cur]) continue;
        dfs2(to, to);
    }
}
int lca (int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    return x;
}
void dfs3 (int cur, int father) {
    w[cur] = d[cur];
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == father) continue;
        dfs3(to, cur);
        w[cur] += w[to];
    }
}

int main () {
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v; i <= n - 1; ++ i) {
        scanf("%d%d", &u, &v);
        add_edge(u, v);
        add_edge(v, u);
    }

    dfs1(1, -1);
    dfs2(1, 1);
    for (int i = 1, x, y; i <= m; ++ i) {
        scanf("%d%d", &x, &y);
        ++ d[x];
        ++ d[y];
        d[lca(x, y)] -= 2;
    }
    dfs3(1, -1);
    int res = 0;
    for (int i = 2; i <= n; ++ i) {
        if (w[i] == 0) res += m;
        if (w[i] == 1) res += 1;
    }
    printf("%d", res);
    return 0;
}
最后修改于: