跳到正文

题解

题解:P5327 [ZJOI2019] 语言

modfish_约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年1月9日
更新
2026年1月9日
篇幅
约 11 分钟
题目
P5327

版本

  1. v1当前

思路

对一个点 xx 统计答案。容易发现即为:覆盖 xx 的所有链 (s,t)(s,t) 的并的大小。

一条链 (s,t)(s,t) 何时覆盖 xx 呢?有两种情况:

  • xx 是 s,ts,t 的 LCA,显然 (s,t)(s,t) 将覆盖 xx。
  • xx 不是 s,ts,t 的 LCA,那么 (s,t)(s,t) 覆盖 xx,当且仅当 s,ts,t 有且仅有一个出现在 xx 的子树中。

上述两个结论都是显然的。于是我们考虑这样操作:

  • 寻找 xx 子树内所有链的端点,如果某条链只有一个端点在 xx 的子树内,那么将链上的点权全部加 11。
  • 寻找所有 LCA 是 xx 的链,将这些链上的点权全部加 11。

最后需要查询全局 00 的个数。

使用线段树+树剖可以做到 O(log⁡2n)O(\log^2 n) 链加,于是问题在于如何快速寻找子树内的链端点。

考虑 dsu on tree,对第 ii 条链记录 bib_i,每遇到链 ii 的一个端点就把 bib_i 异或 11,最后把 bib_i 为 11 的链加 11。处理时先把所有轻儿子递归解决了,并把线段树和 bib_i 都清空,然后解决重儿子,继承其线段树和 bib_i,再扫一遍轻儿子即可。

时间复杂度 O(mlog⁡3n)O(m\log^3 n),但是如果对每条重链单独建树,那么树剖、线段树、dsu on tree 的 log⁡n\log n 都跑不满,所以可以轻松通过,不用卡常。

代码

C++
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int maxn = 1e5 + 5;

int n, q;
int head[maxn], nxt[maxn << 1], to[maxn << 1], cnt = 0;
int fa[maxn], dep[maxn], siz[maxn], son[maxn], dfn[maxn], top[maxn], dnt = 0, len[maxn];
int rt[maxn], us[maxn], vs[maxn];
vector<int> E[maxn], L[maxn];

namespace seg{
int lc[maxn << 2], rc[maxn << 2], mn[maxn << 2], ct[maxn << 2], tag[maxn << 2], tot = 0;
static inline void up(int x){
    if(mn[lc[x]] < mn[rc[x]]) mn[x] = mn[lc[x]], ct[x] = ct[lc[x]];
    else if(mn[lc[x]] > mn[rc[x]]) mn[x] = mn[rc[x]], ct[x] = ct[rc[x]];
    else mn[x] = mn[lc[x]], ct[x] = ct[lc[x]] + ct[rc[x]];
}
static inline void down(int x){
    if(tag[x]){
        mn[lc[x]] += tag[x], mn[rc[x]] += tag[x];
        tag[lc[x]] += tag[x], tag[rc[x]] += tag[x];
        tag[x] = 0;
    }
}
static inline void build(int &x, int l, int r){
    x = ++ tot;
    if(l == r){
        ct[x] = 1;
        return;
    }
    int mid = l + r >> 1;
    build(lc[x], l, mid), build(rc[x], mid + 1, r);
    up(x);
}
static inline void update(int x, int l, int r, int ql, int qr, int k){
    if(ql <= l && r <= qr){
        mn[x] += k, tag[x] += k;
        return;
    }
    down(x);
    int mid = l + r >> 1;
    if(ql <= mid) update(lc[x], l, mid, ql, qr, k);
    if(qr > mid) update(rc[x], mid + 1, r, ql, qr, k);
    up(x);
}
static inline int query(int x){
    return mn[x] ? 0 : ct[x];
}}
static inline void match(int u, int v){
    nxt[++ cnt] = head[u], to[cnt] = v, head[u] = cnt;
}
static inline void dfs(int x, int f){
    fa[x] = f, dep[x] = dep[f] + 1, siz[x] = 1;
    for(int i = head[x]; i; i = nxt[i]){
        int j = to[i];
        if(j == f) continue;
        dfs(j, x);
        siz[x] += siz[j];
        if(siz[j] > siz[son[x]]) son[x] = j;
    }
}
static inline void dfs2(int x, int tp){
    top[x] = tp, dfn[x] = ++ dnt, len[tp] ++;
    if(son[x]) dfs2(son[x], tp);
    for(int i = head[x]; i; i = nxt[i]){
        int j = to[i];
        if(j == fa[x] || j == son[x]) continue;
        dfs2(j, j);
    }
    if(x == tp) seg::build(rt[x], 1, len[x]);
}
int res = 0;
static inline void link_update(int u, int v, int k){
    while(top[u] != top[v]){
        if(dep[top[u]] < dep[top[v]]) swap(u, v);
        res -= seg::query(rt[top[u]]);
        seg::update(rt[top[u]], 1, len[top[u]], 1, dfn[u] - dfn[top[u]] + 1, k);
        res += seg::query(rt[top[u]]);
        u = fa[top[u]];
    }
    if(dep[u] > dep[v]) swap(u, v);
    res -= seg::query(rt[top[u]]);
    seg::update(rt[top[u]], 1, len[top[u]], dfn[u] - dfn[top[u]] + 1, dfn[v] - dfn[top[v]] + 1, k);
    res += seg::query(rt[top[u]]);
}
static inline int LCA(int u, int v){
    while(top[u] != top[v]){
        if(dep[top[u]] < dep[top[v]]) swap(u, v);
        u = fa[top[u]];
    }
    return dep[u] < dep[v] ? u : v;
}
ll ans = 0;
int tag[maxn], st[maxn << 1], tp = 0;
static inline void scan(int x){
    for(int i = 0; i < E[x].size(); i ++){
        int j = E[x][i];
        if(!tag[j]) link_update(us[j], vs[j], 1);
        else link_update(us[j], vs[j], -1);
        st[++ tp] = j;
        tag[j] ^= 1;
    }
    for(int i = head[x]; i; i = nxt[i]){
        int j = to[i];
        if(j == fa[x]) continue;
        scan(j);
    }
}
static inline void dsu(int x){
    for(int i = head[x]; i; i = nxt[i]){
        int j = to[i];
        if(j == fa[x] || j == son[x]) continue;
        dsu(j);
        while(tp){
            int y = st[tp];
            if(tag[y]) link_update(us[y], vs[y], -1);
            tag[y] = 0;
            tp --;
        }
    }
    if(son[x]){
        dsu(son[x]);
        for(int i = head[x]; i; i = nxt[i]){
            int j = to[i];
            if(j == fa[x] || j == son[x]) continue;
            scan(j);
        }
    }
    for(int i = 0; i < E[x].size(); i ++){
        int j = E[x][i];
        if(!tag[j]) link_update(us[j], vs[j], 1);
        else link_update(us[j], vs[j], -1);
        st[++ tp] = j;
        tag[j] ^= 1;
    }
    for(int i = 0; i < L[x].size(); i ++){
        int j = L[x][i];
        link_update(us[j], vs[j], 1);
    }
    ans += max(n - res, 1);
    for(int i = 0; i < L[x].size(); i ++){
        int j = L[x][i];
        link_update(us[j], vs[j], -1);
    }
}

int main(){
    scanf("%d %d", &n, &q);
    res = n;
    for(int i = 1; i < n; i ++){
        int u, v;
        scanf("%d %d", &u, &v);
        match(u, v), match(v, u);
    }
    dfs(1, 0), dfs2(1, 1);
    for(int i = 1; i <= q; i ++){
        int u, v;
        scanf("%d %d", &u, &v);
        us[i] = u, vs[i] = v;
        int lca = LCA(u, v);
        E[u].push_back(i), E[v].push_back(i), L[lca].push_back(i);
    }
    dsu(1);
    printf("%lld\n", (ans - n) / 2);
    return 0;
}
原文