链剖分专题

· · 个人记录

讲一个长链剖分

这个剖分的定义为

有什么用呢?

任何一个节点到其根节点的轻边转换最多是 \sqrt{n} 次的,其形状大概长成一个阶梯形状

还能用来优化树上 dp,去掉一个深度维

一个大小为n,以1为根的树,设d(u,k)是:u子树中,和u节点距离为k的节点个数

对于每个节点u,求最小k,满足d(u,k)最大

最普通的做法是,i节点,深度为k的子节点个数f_{i,k}=\sum\limits_{j \in son[i]}f_{j,k-1}

但是显而易见,这种朴素的方法完全过不了这道题

所以我们用到了长链剖分

先跑一次长链剖分,然后dp

我们可以把重儿子的dp信息强行继承过来,然后暴力合并剩下的轻儿子的dp信息

时间复杂度是O(n)

#define ll int 
vector<ll>To[1000005],dp[1000005];
ll dep[1000005],bson[1000005],ans[1000005]; 
ll a,b,fr,to;
void dfs1(ll x,ll fa)//长链剖分
{
    for(int s=0;s<To[x].size();s++)
    {
        ll to=To[x][s];
        if(to==fa)continue;
        dfs1(to,x);
        if(dep[to]>dep[bson[x]])
        bson[x]=to;
    }
    dep[x]=dep[bson[x]]+1;
}
void dfs2(ll x,ll fa)
{
    if(bson[x])//有孩子
    {
        dfs2(bson[x],x);//先处理重儿子的dp信息
        swap(dp[x],dp[bson[x]]);//把重儿子的dp信息继承过来
        dp[x].push_back(1);//但是继承过来还需要一些改变,dp[i][0]=1(其中的0表示depth),由于vector只能从后面加入数字,所以倒着来存
        ans[x]=ans[bson[x]]+1;//继承
        for(int s=0;s<To[x].size();s++)
        {
            ll to=To[x][s];
            if(to==fa||to==bson[x])continue;
            dfs2(to,x);
            for(int v=dep[to]-1, k=1;v>=0;v--,k++)//暴力合并轻儿子
            {
                // to real dep=v,to dep=dep[to]-v,x dep=dep[to]-v+1,x_real_dep=dep[x]-x_dep
                ll temp=v+dep[x]-dep[to]-1;
                dp[x][temp]+=dp[to][v];
                if(dp[x][temp]>dp[x][dep[x]-ans[x]-1]||dp[x][temp]==dp[x][dep[x]-ans[x]-1]&&k<ans[x])
                {
                    ans[x]=k;
                }
            }
        }

    }
    else
    {
        dp[x].push_back(1);
        ans[x]=0;
    }
    if(dp[x][dep[x]-ans[x]-1]==1)ans[x]=0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>a;
    for(int s=1;s<a;s++)
    {
        cin>>fr>>to;
        To[fr].push_back(to);
        To[to].push_back(fr);
    }
    dfs1(1,0);
    dfs2(1,0);
    for(int s=1;s<=a;s++)
    cout<<ans[s]<<" "<<endl;
}
\text{咕了很久}

但总归还是写了,实链剖分

详解

这里着重讲些细节吧

因为其特殊的设计,Splay 是最适合其的数据结构,也正是这些特殊构造,使得这个结构有大量的细节需要注意

首先其维护的是一个树,而在内部维护一个个链组成的平衡树

满足:

而 access 的作用就是打通该点到根上的虚边,也是所有操作的核心操作

贴个 code

#define check(x) (x==son[f[x]][1])
using namespace std;
ll son[100005][2],f[100005],ans[100005],qu[100005];
bool tag[100005];
ll st[100005];
bool nroot(ll x)// ask Is it not a root?
{
    return son[f[x]][0] == x || son[f[x]][1] == x;
}
void verse(ll x)
{
    swap(son[x][0],son[x][1]);
    tag[x] ^= 1;
}
void pushdown(ll x)
{
    if(tag[x])verse(son[x][0]),verse(son[x][1]),tag[x]=0;
}
void update(ll x)
{
    if(son[x][0])f[son[x][0]] = x;
    if(son[x][1])f[son[x][1]] = x;
    ans[x] = qu[x] ^ ans[son[x][0]] ^ ans[son[x][1]];// the problem i should maintain
}
void turn(ll x)
{
    ll fa = f[x], nw = check(x);
    if(nroot(fa))son[f[fa]][check(fa)]=x;// pay attention ,not " f[f[fa]] != 0" 
    f[x] = f[fa];
    son[fa][nw] = son[x][nw^1];
    son[x][nw^1] = fa;
    update(fa);
    update(x);
}
void splay(ll x)
{
    ll X = x,St = 0;
    st[++St] = X;
    while(nroot(X))st[++St] = X = f[X];
    while(St)pushdown(st[St--]); // pretreatment
    while(nroot(x))
    {
        if(nroot(f[x]))
        {
            if(check(x) == check(f[x]))turn(f[x]);
            else turn(x);
        }
        turn(x);
    }
}
void access(ll x)// so this is the core of this algorithm . and we must let x to the root , and delete something imaginary edge, which may have error with the characters
{
    for(int y = 0; x;x=f[y=x])
    {
        splay(x),son[x][1]=y,update(x);
    }
}
void makeroot(ll x)
{
    access(x);
    splay(x);
    verse(x);
}
ll findroot(ll x)// in the real tree , the root , whom is contacted with x
{
    access(x);splay(x);
    while(son[x][0])pushdown(x),x=son[x][0];
    splay(x);
    return x;
}
void split(ll x,ll y)// to get the list of x to y
{
    makeroot(x);
    access(y);
    splay(y);
}
void link(ll x,ll y)
{
    makeroot(x);
    if(findroot(y) != x)f[x] = y;
} 
void cut(ll x,ll y)
{
    makeroot(x);
    if(findroot(y) == x && f[y] == x && !son[y][0])
    {
    // pay attention here, after findroot(y) ,the x is become a root !
        f[y]=son[x][1]=0;
        update(x);
    }
}
ll n,m; 
int main()
{
    in(n),in(m);
    for(int s=1;s<=n;s++)in(qu[s]);
    while(m--)
    {
        ll in(op),in(x),in(y);
        if(op == 0)
        {
            split(x,y);
            printf("%lld\n",ans[y]);
        }
        if(op == 1)
        {
            link(x,y);
        }
        if(op == 2)
        {
            cut(x,y);
        }
        if(op == 3)
        {
            splay(x);
            qu[x] = y;
            update(x);
        }
    }
    return 0;
}
\huge\text{你要来点题目吗} \text{P2173 [ZJOI2012]网络}

板子题,维护颜色个数个 LCT ,但要注意如何维护 error 1 ,注意到我们如果用 LCT 维护很麻烦,就直接暴力维护就可以了

\text{动态加边维护最小生成树}

假设加入边 (x,y) ,如果 x,y 不联通,就加边,如果联通,我们寻找链 x \leftrightarrow y 上最大值,如果其大于新边,断边,连上新的,否则不做任何操作

\text{LCT维护子树信息 \& P4219 [BJOI2014]大融合}

因为 \text{LCT} 认父不认子的特点,使得 \text{LCT} 难以维护,不妨思考这些虚儿子是怎么产生的,是 \text{access} 产生的,那么,那么,\text{ access } 的时候不妨记录下失去的儿子的信息即可