P6072 『MdOI R1』Path

· · 题解

单 \log 楼上已经有人写了 但似乎跑的很慢 不太理解 我的单 \log 跑的挺快 现在 ( 2022.2.11 ) 是最优解第一 写篇题解纪念一下 qwq

首先任选一个根路径差分 点权为点到根路径边权异或和

问题转化为分割一颗树变成一个子树和剩余部分 使这两部分各选最大异或点对 两部分最大异或点对之和为这种分割方案的答案

分割问题 很直观的一个想法是枚举分割点 也就相当于求全部子树和 rest 的最大异或点对 用 dsu 有双 \log 解法

但是我们要求得是全局答案 这种方案需要求出每个点的答案 是将问题加强了的

考虑解决这类全局支配问题的方法 先找到全局最大点对 这两个点的路径外的点为分割点时显然有一部分最大值必为全局异或最大点对 而另一部分子树内的点是显然比不上子树的根的 给张图比较好理解

红色点对为异或最大点对 注意 现在的点权是经过问题转化后的

那么不难看出黄色的点为分割点显然是要劣于绿色的点为分割点

因此我们只需要处理红色路径四周的点为根的子树和红色路径上点为分割点

我们可以形象地称红色路径为树干

我们发现树干四周的点的子树是互不包含的 因此这里的点只需要 O(1) 次操作 01trie 暴力插暴力清空即可

再考虑树干上的点

树干是弯的还有一个关键点为 lca 不太好处理 我们考虑换根 而换根没有改变树的路径 因此我们树上差分边权转点权的问题转化还是成立的

我们考虑以树干上一个点为根

仔细看这两张图其实是一样的树

问题转化为一条从上到下的链上求子树内异或最大点对加子树外异或最大点对的值

01trie 暴力插 每个点插 O(1) 次

因此总共插 O(n) 次 单次 O(\log V)

总复杂度时间空间都是 O(n\log V)

坑点是 01trie 空间要开够 我写的这个最后会多出一倍的点 01trie 压缩可以空间线性 我不会(

然后就没啥坑了 两发就过了 卡了几发常

这题不开 O2 比开要快(

#include <bits/stdc++.h>
using namespace std;
int n;
const int N=3e4+3;
inline int read()
{
    int x=0;
    char c=getchar();
    while(c<'0'||c>'9')c=getchar();
    while(c<='9'&&c>='0')
    {
        x=(x<<1)+(x<<3)+c-'0';
        c=getchar();
    }
    return x;
}
struct node
{
    int to;
    int nxt;
    int w;
}e[N<<1];
int etot,h[N],w[N];
inline void e_add(int x,int y,int w)
{
    e[++etot].to=y;
    e[etot].nxt=h[x];
    e[etot].w=w;
    h[x]=etot;
}
void get_w(int x,int fa)
{
    for(int i=h[x];i;i=e[i].nxt)
    {
        if(e[i].to!=fa)
        {
            w[e[i].to]=w[x]^e[i].w;
            get_w(e[i].to,x);
        }
    }
}
int trie[N*60+1][2],id[N*60+1];
int maxv,maxn[2];
int tot;
inline void ins(int x,int idx)
{
    bool f[32];
    int p=0;
    for(int i=30;i>=0;i--)
    {
        if(x>=(1<<i))
        {
            x-=(1<<i);
            f[i]=1;
        }
        else
        f[i]=0;
        if(!trie[p][f[i]])trie[p][f[i]]=++tot;
        p=trie[p][f[i]];
    }
    id[p]=idx;
    p=0;
    int res=0;
    for(int i=30;i>=0;i--)
    {
        if(!trie[p][f[i]^1])p=trie[p][f[i]];
        else
        {
            p=trie[p][f[i]^1];
            res+=(1<<i);
        }
    }
    if(res>maxv)
    {
        maxv=res;
        maxn[0]=id[p];
        maxn[1]=idx;
    }
}
inline void get_max()
{
    for(int i=1;i<=n;i++)ins(w[i],i);
}
int fa[N];
void rebuild(int x)
{
    for(int i=h[x];i;i=e[i].nxt)
    {
        if(e[i].to!=fa[x])
        {
            fa[e[i].to]=x;
            rebuild(e[i].to);
        }
    }
}
inline void clear(int p)
{
    if(trie[p][0])
    {
        clear(trie[p][0]);
        trie[p][0]=0;
    }
    if(trie[p][1])
    {
        clear(trie[p][1]);
        trie[p][1]=0;
    }
}
int son[N];
int all_max,ans;
void dfs(int x)
{
    ins(w[x],x);
    for(int i=h[x];i;i=e[i].nxt)
    if(e[i].to!=fa[x])dfs(e[i].to);
}
int ans_t[N];
inline void lxl(int rt,int l_end)
{
    int x=l_end;
    while(x!=rt)
    {
        son[fa[x]]=x;
        x=fa[x];
    }
    while(x)
    {
        for(int i=h[x];i;i=e[i].nxt)
        {
            if(e[i].to!=fa[x]&&e[i].to!=son[x])
            {
                clear(0);
                maxv=-1;
                dfs(e[i].to);
                ans=max(ans,all_max+maxv);
            }
        }
        x=son[x];
    }
    clear(0);
    maxv=-1;
    x=rt;
    while(x)
    {
        ans_t[x]=maxv;
        for(int i=h[x];i;i=e[i].nxt)
        {
            if(e[i].to!=fa[x]&&e[i].to!=son[x])
            {
                dfs(e[i].to);
            }
        }
        ins(w[x],x);
        x=son[x];
    }
    clear(0);
    maxv=-1;
    x=l_end;
    while(x)
    {
        for(int i=h[x];i;i=e[i].nxt)
        {
            if(e[i].to!=fa[x]&&e[i].to!=son[x])
            {
                dfs(e[i].to);
            }
        }
        ins(w[x],x);
        ans_t[x]+=maxv;
        ans=max(ans,ans_t[x]);
        x=fa[x];
    }
}
int main()
{
    n=read();
    for(int i=1;i<n;i++)
    {
        int x=read(),y=read(),w=read();
        e_add(x,y,w);
        e_add(y,x,w);
    }
    maxv=-1;
    ans=-1;
    get_w(1,0);
    get_max();
    int uu1=maxn[0],uu2=maxn[1];
    all_max=maxv;
    rebuild(maxn[0]);
    lxl(uu1,uu2);
    printf("%d",ans);
    return 0;
}