跳到正文

题解

题解:P4115 Qtree4

_CuteRalsei_约 26 分钟
原文
文章信息与版本

文章信息

发布
2026年9月17日
收录
2026年9月17日
更新
2026年11月10日
篇幅
约 26 分钟
获赞
1
题目
P4115

版本

  1. v1当前
\gdef \so { \implies } \gdef \P { \mathbb{P} } \gdef \id { \mathrm{id} } \gdef \lcm { \mathrm{lcm} } \gdef \or { \mathbin{ \hbox{ \rm or } } } \gdef \xor { \mathbin{ \hbox{ \rm xor } } } \gdef \and { \mathbin{ \hbox{ \rm and } } } \gdef \inv #1{ { #1 ^ {-1} } } \gdef \abs #1{ \left \lvert #1 \right \rvert } \gdef \pare #1{ \left \lparen #1 \right \rparen } \gdef \paren (#1){ \left \lparen #1 \right \rparen } \gdef \brack [#1]{ \left \lbrack #1 \right \rbrack } \gdef \brace #1{ \left \lbrace #1 \right \rbrace } \gdef \ceil #1{ \left \lceil #1 \right \rceil } \gdef \floor #1{ \left \lfloor #1 \right \rfloor } \gdef \Vert #1{ \left \lVert #1 \right \rVert } \gdef \vector #1#2{ \begin{bmatrix} #1 \\ #2 \end{bmatrix} } % \gdef \FirstStirling #1#2{ \begin{bmatrix} #1 \\ #2 \end{bmatrix} } % \gdef \SecondStirling #1#2{ \begin{Bmatrix} #1 \\ #2 \end{Bmatrix} } \gdef \zc #1#2{\lfloor\frac{#1}{#2}\rfloor} \gdef \dzc #1#2{\lfloor\dfrac{#1}{#2}\rfloor} \gdef \eps {\varepsilon} \gdef \ph {\varphi} \gdef \ba {\begin{aligned}} \gdef \ea {\end{aligned}} \gdef \lcm {\mathrm{lcm}} \gdef \d {\cdot} \gdef \etc {\cdots} \gdef \Etc {\cdots\cdots} \gdef \O {\mathcal{O}} \gdef \updots {\substack{\cdot \hspace{0.6em}\\[-0.7em] \cdot \\[-0.7em]\hspace{0.6em} \cdot\\[-0.7em]}} \gdef \bc {\begin{cases}} \gdef \ec {\end{cases}} \gdef \mat#1{\begin{bmatrix}#1\end{bmatrix}}

树上集合直径类型题目

P2056 [ZJOI2007] 捉迷藏 以及 AT_abc460_f [ABC460F] Farthest Pair Query

本题弱化版。

考虑对于集合 S,TS,T,SS 的直径为 uS→vSu_S\to v_S,TT 的直径为 uT→vTu_T\to v_T。

那么 S∪TS\cup T 的直径 uS∪T→vS∪Tu_{S\cup T}\to v_{S\cup T},满足 uS∪T∈{uS,vS,uT,vT},vS∪T∈{uS,vS,uT,vT},uS∪T≠vS∪Tu_{S\cup T}\in\{u_S,v_S,u_T,v_T\},v_{S\cup T}\in\{u_S,v_S,u_T,v_T\},u_{S\cup T}\neq v_{S\cup T}。

于是直接维护即可。查询 O(1)O(1),修改 O(log⁡n)O(\log n)。

code
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e5+10,inf=1e18;
struct edge{
    int v,w;
};
vector<edge> T[maxn];
int fa[maxn],siz[maxn],hson[maxn],g[maxn],top[maxn],ord,dfn[maxn],tid[maxn],L[maxn],R[maxn],depw[maxn],chain[maxn],ch,dep[maxn];
void dfs1(int f,int u,int wf){
    siz[u]=1;
    fa[u]=f;
    depw[u]=depw[f]+wf;
    dep[u]=dep[f]+1;
    int H=0,mize=0;
    for(auto [v,w]:T[u]){
        if(v==f)continue;
        dfs1(u,v,w);
        siz[u]+=siz[v];
        if(siz[v]>mize){
            H=v;
            mize=siz[v];
        }
    }
    hson[u]=H;
}
void dfs2(int Top,int u){
    if(Top==u)ch++;
    chain[u]=ch;
    top[u]=Top;
    dfn[u]=++ord;
    tid[ord]=u;
    L[chain[u]]=min(L[chain[u]],ord);
    R[chain[u]]=max(R[chain[u]],ord);
    if(hson[u])dfs2(Top,hson[u]);
    for(auto [v,w]:T[u]){
        if(v==fa[u]||v==hson[u])continue;
        dfs2(v,v);
    }
}
int lca(int u,int v){
    while(top[u]!=top[v]){
        if(dep[top[u]]<dep[top[v]])swap(u,v);//dep 大优先
        u=fa[top[u]];
    }
    if(dep[u]>dep[v])return v;
    return u;
}
int dis(int u,int v){
    return depw[u]+depw[v]-depw[lca(u,v)]*2;
}
struct info{
    int u,v,Dis;//save dis
    friend info merge(info a,info b){
        int node[4]={a.u,a.v,b.u,b.v},maxd=-inf;
        info ans={-1,-1,-inf};
        for(int i=0;i<4;i++){
            if(node[i]==-1)continue;
            for(int j=i+1;j<4;j++){
                if(node[j]==-1)continue;
                int d=dis(node[i],node[j]);
                if(d>maxd){
                    maxd=d;
                    ans={node[i],node[j],d};
                }
            }
        }
        return ans;
    }
};
struct sgt{
    int l,r;
    info dat;
}t[maxn*4];
bitset<maxn> col;//1=white
#define ls id<<1
#define rs id<<1|1
void up(int id){t[id].dat=merge(t[ls].dat,t[rs].dat);}
void build(int id,int l,int r){
    t[id].l=l,t[id].r=r;
    if(l==r){
        t[id].dat={tid[l],tid[l],0};
        return;
    }
    int mid=(l+r)>>1;
    build(ls,l,mid);
    build(rs,mid+1,r);
    up(id);
}
void update(int id,int pos,bool nc){
    if(t[id].l>pos||t[id].r<pos)return;
    if(t[id].l==t[id].r&&t[id].l==pos){
        if(nc){//
            t[id].dat={tid[pos],tid[pos],0};
        }
        else t[id].dat={-1,-1,-inf};
        return;
    }
    update(ls,pos,nc);
    update(rs,pos,nc);
    up(id);
}
signed main() 
{
    col.set();
    memset(L,0x7f,sizeof(L));
    memset(R,-1,sizeof(R));
    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v,w;
        cin>>u>>v>>w;
        T[u].push_back({v,w});
        T[v].push_back({u,w});
    }
    int rt=1;
    dfs1(rt,rt,0);
    dfs2(rt,rt);
    build(1,1,n);
    #ifdef LOCAL
    cerr<<"prepared"<<endl;
    #endif
    int q;
    cin>>q;
    while(q--){
        char opt;
        cin>>opt;
        if(opt=='A'){
            info ans=t[1].dat;
            if(ans.u==-1&&ans.v==-1){
                cout<<"They have disappeared.\n";
            }
            else cout<<ans.Dis<<endl;
        }
        else{//change color
            int x;
            cin>>x;
            col[x]=!col[x];
            update(1,dfn[x],col[x]);
        }
    }
    return 0;
}

P4115 Qtree4 以及 SP2666 QTREE4 - Query on a tree IV

负权,无法贪心。

考虑 dpu,0dp_{u,0} 表示 uu 子树内到 uu 最远点白点。

那么:

dpu,0=max⁡(max⁡v∈son(u)dpv+wu,v,{0,cu=1−∞,cu=0) dp_{u,0}=\max\paren(\max_{v\in son(u)}dp_v+w_{u,v},\bc0,&c_u=1\\-\infty,&c_u=0\ec)

即设 HuH_u 表示 uu 的重儿子,有

gu,0=max⁡(max⁡v∈son(u),v≠Hudpv+wu,v,{0,cu=1−∞,cu=0)g_{u,0}=\max\paren(\max_{v\in son(u),v\neq H_u}dp_v+w_{u,v},\bc0,&c_u=1\\-\infty,&c_u=0\ec) dpu,0=max⁡(max⁡(gu,dpHu+wu,Hu),{0,cu=1−∞,cu=0)dp_{u,0}=\max\left(\max(g_u,dp_{H_u}+w_{u,H_u}),\bc0,&c_u=1\\-\infty,&c_u=0\ec\right)

dpu,1dp_{u,1} 表示答案,即 uu 子树内最远两个白点的距离。

gu,1=max⁡(max⁡v∈son(u)dpv,1,max⁡v1,v2∈son(u),v1≠v2,v1,v2≠Hu(dpv1,0+dpv2,0+wu,v1+wu,v2),{max⁡v∈son(u),v≠Hu(dpv,0+wv,u),cu=1−∞,cu=0)g_{u,1}=\max\pare{\max_{v\in son(u)}dp_{v,1},\max_{v_1,v_2\in son(u),v_1\neq v_2,v_1,v_2\neq H_u} (dp_{v_1,0}+dp_{v_2,0}+w_{u,v_1}+w_{u,v2}),\bc\displaystyle\max_{v\in son(u),v\neq H_u}(dp_{v,0}+w_{v,u}),&c_u=1\\-\infty,&c_u=0\ec} dpu,1=max⁡(dpH,1,gu,1,dpHu,0+wu,Hu+gu,0)dp_{u,1}=\max\paren(dp_{H,1},g_{u,1},dp_{H_u,0}+w_{u,H_u}+g_{u,0})

设 dp 矩阵为 [dpu,0,dpu,1,0]\mat{dp_{u,0},dp_{u,1},0},则希望用 (max⁡,+)(\max,+) 矩阵转移,即 [dpHu,0,dpHu,1,0]×M=[dpu,0,dpu,1,0]\mat{dp_{H_u,0},dp_{H_u,1},0}\times M=\mat{dp_{u,0},dp_{u,1},0}。

可以解得 M=[wu,Huwu,Hu+gu,0−∞−∞0−∞gu,0gu,10]M=\mat{w_{u,H_u}&w_{u,H_u}+g_{u,0}&-\infty\\-\infty&0&-\infty \\g_{u,0}&g_{u,1}&0}。

考虑求这个 gu,0,gu,1g_{u,0},g_{u,1}。

gu,0g_{u,0} 较好维护,gu,1g_{u,1} 只需要维护最大值和次大值。

code(马蜂已格式化)
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e5 + 10, inf = 1e18;
struct edge {
    int v, w;
};
template <typename T>
struct multiv {
    multiset <T> st;
    void insert(T x){st.insert(x);}
    void erase(T x){
        if(st.find(x) == st.end()){
            // cout<<"can't erase"<<x<<endl;
            exit(1);
            return;
        }
        // sth wrong...
        st.erase(st.find(x));
    }
    void erase_all(T x){st.erase(x);}
    void update(T x, T y){
        auto it = st.find(x);
        if(it == st.end()){
            st.insert(y);
            return;
        } st.erase(it);
        st.insert(y);
    }
    T max(){return st.empty() ?-inf :*st.rbegin();}
    T smax(){return st.size() <= 1 ?-inf :*(++st.rbegin());}//反向迭代器
};
struct mat {
    //(max,+) Matrix
    int a[3][3];
    bool ie = 0;
    mat(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                a[i][j] = -inf;
            }
        }
    }
    mat(bool IE){ie = IE;}
    mat(int x, int y, int z){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                a[i][j] = -inf;
            }
        }
        a[0][0] = x;
        a[0][1] = y;
        a[0][2] = z;
    }
    inline mat(int x, int y, int z, int t, int o, int p, int q, int r, int s){
        a[0][0] = x;
        a[0][1] = y;
        a[0][2] = z;
        a[1][0] = t;
        a[1][1] = o;
        a[1][2] = p;
        a[2][0] = q;
        a[2][1] = r;
        a[2][2] = s;
    }
    inline friend mat mul(mat a, mat b){
        mat c;
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                for(int k = 0; k < 3; k++){
                    c.a[i][j] = max(c.a[i][j], a.a[i][k] + b.a[k][j]);
                }
            }
        }
        return c;
    }
    inline friend mat operator * (mat a, mat b){
        if(!a.ie && !b.ie)return mul(a, b);
        else if(!a.ie && b.ie)return a;
        else if(a.ie)return b;
    }
    inline friend void write(mat a){
        cout << "Matrix:" << endl;
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                cout << a.a[i][j] << ' ';
            } cout<<endl;
        }
    }
} mt[maxn];
vector <edge> T[maxn];
int fa[maxn], siz[maxn], hson[maxn], top[maxn], ord, dfn[maxn], tid[maxn], L[maxn], R[maxn], depw[maxn], chain[maxn], tail[maxn], ch, dep[maxn], dp[maxn][2], g[maxn][2], wh[maxn], sg[maxn], fw[maxn];
multiv <int> g1[maxn][2];
bitset <maxn> c;
//1=white
void dfs1(int f, int u, int wf){
    // cout<<"dfs1: vis"<<u<<endl;
    siz[u] = 1;
    fa[u] = f;
    depw[u] = depw[f] + wf;
    dep[u] = dep[f] + 1;
    dp[u][0] = -inf;
    int H = 0, mize = 0;
    for(auto [v, w] : T[u]){
        if(v == f)continue;
        dfs1(u, v, w);
        siz[u] += siz[v];
        dp[u][0] = max(dp[u][0], dp[v][0] + w);
        if(siz[v] > mize){
            H = v;
            mize = siz[v];
            wh[u] = w;
        }
    }
    // dp[u][0]=max(dp[u][0],c[u]?0:-inf);
    hson[u] = H;
}
void DP(int u){
    g[u][0] = max(g1[u][0].max(), c[u] ? 0 : -inf);
    dp[u][0] = max(g[u][0], dp[hson[u]][0] + wh[u]);
    g[u][1] = max({g1[u][1].max(), g1[u][0].max() + g1[u][0].smax(), c[u] ? max(0ll, g1[u][0].max()) :-inf});
    dp[u][1] = max({dp[hson[u]][1], g[u][1], dp[hson[u]][0] + g[u][0] + wh[u]});
    mt[u] = mat(wh[u], wh[u] + g[u][0], -inf, -inf, 0, -inf, g[u][0], g[u][1], 0);
}
void dfs2(int Top, int u){
    // cout<<u<<"heavy son:"<<hson[u]<<endl;
    dp[u][1] = g[u][0] = g[u][1] = g[u][0] = -inf;
    top[u] = Top;
    dfn[u] = ++ord;
    tid[ord] = u;
    tail[top[u]] = max(tail[top[u]], ord);
    if(hson[u]){
        dfs2(Top, hson[u]);
    }
    for(auto [v, w] : T[u]){
        if(v == fa[u] || v == hson[u])continue;
        fw[v] = w;
        dfs2(v, v);
        g[u][0] = max(g[u][0], dp[v][0] + w);
        g1[u][0].insert(dp[v][0] + w);
        g1[u][1].insert(dp[v][1]);
    } DP(u);
}
struct sgt {
    int l, r;
    mat m;
} t[maxn*4];
#define ls id << 1
#define rs id << 1 | 1
void up(int id){t[id].m = t[rs].m * t[ls].m;}
void build(int id, int l, int r){
    t[id].l = l, t[id].r = r;
    if(l == r){
        t[id].m = mt[tid[l]];
        return;
    }
    int mid = (l + r) >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
    up(id);
}
void update(int id, int pos, int u){
    if(t[id].l > pos || t[id].r < pos)return;
    if(t[id].l == t[id].r && t[id].l == pos){
        t[id].m = mt[u];
        return;
    } update(ls,pos,u);
    update(rs, pos, u);
    up(id);
}
mat query(int id, int l, int r){
    if(t[id].l > r || l > t[id].r)return mat(1);
    if(l <= t[id].l && t[id].r <= r)return t[id].m;
    return query(rs, l, r) * query(ls, l, r);
}
void modify(int u){
    c[u] = !c[u];
    update(1, dfn[u], u);
    while (u){
        mat dp0, oldm, newm;
        //需要 dp0满足 dp0*mt[leaf]=[dp[leaf][0],dp[leaf][1],0]
        int w = fw[top[u]], fat = fa[top[u]], tp = top[u], odp0, odp1, ndp0, ndp1;
        if(fat){
            dp0 = mat(dp[tid[tail[tp]]][0], dp[tid[tail[tp]]][1], 0);
            oldm = dp0 * query(1, dfn[tp], tail[tp]);
        } DP(u);
        update(1, dfn[u], u);
        if(fat){
            dp0 = mat(dp[tid[tail[tp]]][0], dp[tid[tail[tp]]][1], 0);
            odp0 = oldm.a[0][0];
            odp1 = oldm.a[0][1];
            g1[fat][0].erase(odp0 + w);
            g1[fat][1].erase(odp1);
            newm = dp0 * query(1, dfn[tp], tail[tp]);
            ndp0 = newm.a[0][0];
            ndp1 = newm.a[0][1];
            g1[fat][0].insert(ndp0 + w);
            g1[fat][1].insert(ndp1);
            // cout<<"u="<<u<<" ndp0="<<ndp[0]<<" ndp1="<<ndp[1]<<endl;
        }
        u = fat;
    }
}
#define endl '\n'
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    c.set();
    int n, wc;
    cin >> n;
    wc = n;
    for(int i = 1; i < n; i++){
        int u, v, w;
        cin >> u >> v >> w;
        T[u].push_back({v, w});
        T[v].push_back({u, w});
    }
    int rt = 1;
    dp[0][0] = dp[0][1] = g[0][0] = g[0][1] = -inf;
    dfs1(0, rt, 0);
    // cout<<"dfs1"<<endl;
    dfs2(rt, rt);
    // cout<<"dfs2"<<endl;
    build(1, 1, n);
    // cout<<"prepared"<<endl;
    int q;
    cin >> q;
    while (q--){
        char opt;
        cin >> opt;
        if(opt == 'A'){
            if(wc == 0){
                cout << "They have disappeared.\n";
                continue;
            }
            mat lf = mat(dp[tid[tail[top[1]]]][0], dp[tid[tail[top[1]]]][1], 0);
            // write(query(1,dfn[1],tail[1]));
            mat ans = lf * query(1, dfn[1], tail[1]);
            // write(ans);
            if(wc == 1)cout << 0 << endl;
            else cout << max(0ll, ans.a[0][1]) << endl;
        }
        else {
            //change color
            int x;
            cin >> x;
            if(c[x])wc--;
            else wc++;
            modify(x);
        }
        // cout<<wc<<endl;
    }
    return 0;
}
原文