题解 P7735 【[NOI2021] 轻重边】

· · 题解

来自退役选手的场外口胡,一个树链剖分的解法。

思路过程

看到操作1,把两点路径上所有边变重,连接这些点的其他边变虚,不难直接想到LCT的Split操作。(后文直接将这个操作叫做Split)

由于去打NOI的学弟一直没学LCT(本来要给他讲的,学长说考不了就没让我讲),所以考虑下有没有其他能维护边的轻重性的做法。

首先有一个基本的想法:因为树的一个节点最多只有一个父亲,所以考虑到把重边存进他所连接的较深节点中。即:一个节点的权值0或1​​表示它连接它父亲的边的轻重性。这样两点路径上的重边数,就是路径上除LCA以外的点权和,不难想到用树链剖分和线段树去维护。

(为区分树链剖分的轻重链和题目中的轻重边,后文将树链剖分的轻/重写作虚/实。)

接下来的问题是如何借助树剖的结果完成Split操作和重边计数。显然,对于Split的重链和实链重合的部分,只需要区间赋值为1​,直接用线段树完成,难点在于将重边变轻和对于虚边上重边的维护。仔细模拟几次Split的操作,不难看出,无论如何折腾这棵树,一个点的重儿子最多有两个,这样我们可以直接使用数组vis[]记录节点u​​​是否有虚儿子是重儿子(后文简称为虚重儿子),然后再开数组记录每个点的虚重儿子是谁,这样只需要解决这个数组的维护了。

考虑暴力。虚重儿子的产生来自于Split操作时,每段实链的链顶的父亲。根据树链剖分的性质,Split的路径包含的实链最多\log n​段,所以一次Split产生的虚重儿子也最多\log n​​个。因为一个虚重儿子只会被消除一次,总消除次数是n \log n​。产生虚重儿子的操作在跳虚边时暴力修改,而消除操作是在重链与实链重合的部分进行,为方便找到实链上有虚重儿子的点,即区间内哪些点的vis[]值为1​,可以使用另一个线段树维护,在树上走路时只进入有1​的子树,到叶子上再暴力即可,总复杂度为n \log ^ 2 n​​​。

程序实现

程序实现部分,Split操作和查询,大体的实现框架都是先求出LCA,再从两个节点往上一次跳一整段实链,跳的时候对线段树进行操作即可。注意当两个点跳到和LCA同一实链后会有很多情况,需要分类讨论。

最后的注释是手造的样例,还算比较强

以下代码为90pts,经过各种常数优化已经AC,不过不优美就不放了。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int MAXN = 1e5 + 5;

int n, m;
int idx[MAXN], bac[MAXN], fa[MAXN], depth[MAXN], siz[MAXN], son[MAXN], top[MAXN], icnt;
int to[MAXN << 1], nx[MAXN << 1], hd[MAXN << 1], ecnt;

void AddEdge(int u, int v) {
    to[++ecnt] = v; nx[ecnt] = hd[u]; hd[u] = ecnt;
    to[++ecnt] = u; nx[ecnt] = hd[v]; hd[v] = ecnt;
}

void DFS1(int u, int f, int d) {
    fa[u] = f;
    depth[u] = d;
    siz[u] = 1;
    for (int i = hd[u]; i; i = nx[i]) {
        int v = to[i];
        if (v == f) continue;
        DFS1(v, u, d + 1);
        siz[u] += siz[v];
        if (siz[v] > siz[son[u]]) son[u] = v;
    }
}

void DFS2(int u, int t) {
    top[u] = t;
    idx[u] = ++icnt;
    bac[idx[u]] = u;
    if (!son[u]) return;
    DFS2(son[u], t);
    for (int i = hd[u]; i; i = nx[i]) {
        int v = to[i];
        if (v == fa[u] || v == son[u]) continue;
        DFS2(v, v);
    }
}

namespace Tree1{//whether "u" is a heavy son
    int tag[MAXN << 2], val[MAXN << 2];

    void PushDown(int x, int l, int r) {
        int mid = (l + r) >> 1;
        if (~tag[x]) {
            val[x << 1] = tag[x] * (mid - l + 1);
            tag[x << 1] = tag[x];
            val[x << 1 | 1] = tag[x] * (r - mid);
            tag[x << 1 | 1] = tag[x];
            tag[x] = -1;
        }
    }

    void Assign(int x, int l, int r, int k, int nl, int nr) {
        if (l == nl && r == nr) {
            tag[x] = k;
            val[x] = k * (r - l + 1);
            return;
        }
        int mid = (nl + nr) >> 1;
        PushDown(x, nl, nr);
        if (r <= mid) Assign(x << 1, l, r, k, nl, mid);
        else if (l > mid) Assign(x << 1 | 1, l, r, k, mid + 1, nr);
        else Assign(x << 1, l, mid, k, nl, mid), Assign(x << 1 | 1, mid + 1, r, k, mid + 1, nr);
        val[x] = val[x << 1] + val[x << 1 | 1];
    }

    int Query(int x, int l, int r, int nl, int nr) {
        if (l == nl && r == nr) return val[x];
        int mid = (nl + nr) >> 1;
        PushDown(x, nl, nr);
        if (r <= mid) return Query(x << 1, l, r, nl, mid);
        else if (l > mid) return Query(x << 1 | 1, l, r, mid + 1, nr);
        else return Query(x << 1, l, mid, nl, mid) + Query(x << 1 | 1, mid + 1, r, mid + 1, nr);
    }
}

int hv[MAXN][3], hcnt[MAXN];

namespace Tree2{//whether "u" has a heavy son out of the real chain
    int val[MAXN << 2];

    void Clean(int u) {
        int p = idx[u];
        Tree1::Assign(1, p, p, 0, 1, n);
    }

    void Modify(int x, int p, int k, int nl, int nr) {
        if (nl == nr) {
            val[x] = k;
            return;
        }
        int mid = (nl + nr) >> 1;
        if (p <= mid) Modify(x << 1, p, k, nl, mid);
        else Modify(x << 1 | 1, p, k, mid + 1, nr);
        val[x] = val[x << 1] + val[x << 1 | 1];
    }

    void Solve(int x, int l, int r, int nl, int nr) {
        if (l == nl && r == nr) {
            if (nl == nr) {
                int u = bac[nl];
                while (hcnt[u]) Clean(hv[u][hcnt[u]--]);
                val[x] = 0;
                return;
            }
            int mid = (nl + nr) >> 1;
            if (val[x << 1]) Solve(x << 1, nl, mid, nl, mid);
            if (val[x << 1 | 1]) Solve(x << 1 | 1, mid + 1, nr, mid + 1, nr);
            val[x] = 0;
        } else {
            int mid = (nl + nr) >> 1;
            if (r <= mid) Solve(x << 1, l, r, nl, mid);
            else if (l > mid) Solve(x << 1 | 1, l, r, mid + 1, nr);
            else Solve(x << 1, l, mid, nl, mid), Solve(x << 1 | 1, mid + 1, r, mid + 1, nr);
            val[x] = val[x << 1] + val[x << 1 | 1];
        }
    }
}

void Reset() {
    memset(hd, 0, sizeof(hd));
    ecnt = 0;
    icnt = 0;
    memset(son, 0, sizeof(son));
    memset(Tree1::val, 0, sizeof(Tree1::val));
    memset(Tree1::tag, -1, sizeof(Tree1::tag));
    memset(Tree2::val, 0, sizeof(Tree2::val));
    memset(hcnt, 0, sizeof(hcnt));
}

int LCA(int x, int y) {
    while (top[x] != top[y]) {
        if (depth[top[x]] > depth[top[y]]) swap(x, y);
        y = fa[top[y]];
    }
    if (depth[x] > depth[y]) swap(x, y);
    return x;
}

void Modify(int x, int y) {
    int lca = LCA(x, y);
    int las1 = 0;
    while (top[lca] != top[x]) {
        Tree2::Solve(1, idx[top[x]], idx[x], 1, n);
        if (las1) hv[x][++hcnt[x]] = las1, Tree2::Modify(1, idx[x], 1, 1, n), Tree1::Assign(1, idx[las1], idx[las1], 1, 1, n);
        if (son[x]) Tree1::Assign(1, idx[son[x]], idx[son[x]], 0, 1, n);
        Tree1::Assign(1, idx[top[x]], idx[x], 1, 1, n);
        las1 = top[x];
        x = fa[las1];
    }
    int las2 = 0;
    while (top[lca] != top[y]) {
        Tree2::Solve(1, idx[top[y]], idx[y], 1, n);
        if (las2) hv[y][++hcnt[y]] = las2, Tree2::Modify(1, idx[y], 1, 1, n), Tree1::Assign(1, idx[las2], idx[las2], 1, 1, n);
        if (son[y]) Tree1::Assign(1, idx[son[y]], idx[son[y]], 0, 1, n);
        Tree1::Assign(1, idx[top[y]], idx[y], 1, 1, n);
        las2 = top[y];
        y = fa[las2];
    }
    if (depth[x] > depth[y]) swap(x, y), swap(las1, las2);
    Tree2::Solve(1, idx[x], idx[y], 1, n);
    if (las1) hv[x][++hcnt[x]] = las1, Tree2::Modify(1, idx[x], 1, 1, n), Tree1::Assign(1, idx[las1], idx[las1], 1, 1, n);
    if (las2) hv[y][++hcnt[y]] = las2, Tree2::Modify(1, idx[y], 1, 1, n), Tree1::Assign(1, idx[las2], idx[las2], 1, 1, n);
    if (son[y]) Tree1::Assign(1, idx[son[y]], idx[son[y]], 0, 1, n);
    if (x != y) Tree1::Assign(1, idx[x] + 1, idx[y], 1, 1, n);
    if (fa[x]) {
        Tree1::Assign(1, idx[x], idx[x], 0, 1, n);
        if (x != son[fa[x]]) {
            if (hcnt[fa[x]] == 2) {
                if (hv[fa[x]][2] == x) {
                    hcnt[fa[x]]--; 
                    Tree2::Modify(1, idx[fa[x]], 0, 1, n);
                } else if (hv[fa[x]][1] == x) {
                    hcnt[fa[x]]--; 
                    hv[fa[x]][1] = hv[fa[x]][2];
                    Tree2::Modify(1, idx[fa[x]], 0, 1, n);
                }
            } else if (hcnt[fa[x]] == 1) {
                if (hv[fa[x]][1] == x) {
                    hcnt[fa[x]]--;
                    Tree2::Modify(1, idx[fa[x]], 0, 1, n);
                }
            }
        }
    }
}

int Query(int x, int y) {
    int sum = 0;
    while (top[x] != top[y]) {
        if (depth[top[x]] > depth[top[y]]) swap(x, y);
        sum += Tree1::Query(1, idx[top[y]], idx[y], 1, n);
        y = fa[top[y]];
    }
    if (depth[x] > depth[y]) swap(x, y);
    if (x != y) sum += Tree1::Query(1, idx[x] + 1, idx[y], 1, n);
    return sum;
}

void Display() {
    // for (int i = 1; i <= n; i++) cerr << Tree1::Query(1, idx[i], idx[i], 1, n) << " ";
    // cerr << "\n";
}

void Work() {
    Reset();
    cin >> n >> m;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        AddEdge(x, y);
    }
    DFS1(1, 0, 1);
    DFS2(1, 1);
    // for (int i = 1; i <= n; i++) cerr << son[i] << " ";
    // cerr << "\n";
    for (int i = 1; i <= m; i++) {
        int op, x, y;
        cin >> op >> x >> y;
        if (op == 1) Modify(x, y), Display();
        else cout << Query(x, y) << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    // freopen("edge.in", "r", stdin);
    // freopen("edge.out", "w", stdout);
    int T;
    cin >> T;
    while (T--) Work();
    return 0;
}
/*
1
8 8
1 2
1 3
3 4
3 7
4 5
4 6
7 8
1 5 8
2 2 8
1 2 8
2 5 8
2 6 8
1 6 7
2 2 8
2 5 8
*/
/*
2
3
2
2
2
*/