题解 P3366 【【模板】最小生成树】

· · 题解

用LCT求最小生成树是一个非常实用的思想,(个人感觉),动态连边,枚举,用Splay维护某种最值,每次连一条边,若其构成环,则环上断边,不难发现,这样操作后仍然为一棵树。

那么先简单的介绍一下LCT

它可以动态的维护一棵树,支持加边,删边,还可以维护每一条链上的许多信息 ,同时,它的链也是动态的(您想维护那一条链就可以维护那一条链)

(比如维护一条链上的最大值)

回到题面,如果用LCT,我们可以想到一种比较直观的想法:

对于每条边,如果其连接了两个联通块,就连边,否则,其相当于在树上的一条返祖边,那么会构成环,我实际上只需要找到环上边权最大的边然后断边即可

具体实现呢?

首先记录当前答案:ans

对于每一条边,如果这条边它连接了两个现在并不联通的联通块,那么我就直接连边,但是因为实际上如果要用LCT上的树点来维护,其父亲到其的边会比较麻烦处理

(动态树它动来动去...,还经常换根,所以不好想树剖那样把边权映射到点上)

所以比较暴力的想法就是,把当前的边当作一个点,那么其编号为n+id

然后这个点的点权为其边权 ,这个时候连边。假设其沟通了x,y,即:

w[id + n] = z; //w表示这个点的点权
link( x, id + n ), link( id + n, y );

否则,这条边是树上的一条返祖边,如果连接,那么x-y会形成环,这个时候需要断掉最大的边。

于是,我们需要用LCT维护链上最大边权(按照之前讲的把边当作点,要消去点对链造成的影响,我们需要将树点的点权设为0)

然后先把x-y的链split出来

然后记录一个id表示点权最大的点的编号。

这个时候,我们判断,如果当前要加入的边比现在最大的边权(边权就是点权,前文加粗部分写到了)还要大,那么显然不能加入这条边

否则,我们就先把最大边所代表的点Splay上去,然后让其两个儿子都不认它,这样我们就完成了断边。

然后再link( x, Id+n), \quad link(Id+n,y)

上代码咯:QAQ

#include<bits/stdc++.h>
using namespace std;
int read() {
    char cc = getchar(); int cn = 0, flus = 1;
    while(cc < '0' || cc > '9') {  if( cc == '-' ) flus = -flus;  cc = getchar();  }
    while(cc >= '0' && cc <= '9')  cn = cn * 10 + cc - '0', cc = getchar();
    return cn * flus;
}
const int N = 2e5 + 5005;
#define ls(x) t[x].son[0]
#define rs(x) t[x].son[1]
struct LCT {
    int son[2], mx, id, fa;
    bool mark;
}t[N]; 
int w[N], n, m, Idnet, ans;
void pushup( int x ) { //每次下传都需要更新最大点权,
    t[x].id = x, t[x].mx = w[x];
    if( t[ls(x)].mx > t[x].mx ) t[x].mx = t[ls(x)].mx, t[x].id = t[ls(x)].id;
    if( t[rs(x)].mx > t[x].mx ) t[x].mx = t[rs(x)].mx, t[x].id = t[rs(x)].id;
}
bool isroot( int x ) {
    return ( rs(t[x].fa) != x ) && ( ls(t[x].fa) != x );
}
void pushmark( int x ) { //下传翻转标记
    if( t[x].mark ) {
        t[x].mark = 0, t[ls(x)].mark ^= 1, t[rs(x)].mark ^= 1;
        swap( ls(x), rs(x) );
    }
}
void rotate( int x ) { //旋转
    int f = t[x].fa, ff = t[f].fa, qwq = ( rs(f) == x );
    t[x].fa = ff;
    if( !isroot(f) ) t[ff].son[(rs(ff) == f)] = x; //如果父亲不为根才改爷爷 
    t[t[x].son[qwq ^ 1]].fa = f, t[f].son[qwq] = t[x].son[qwq ^ 1],
    t[f].fa = x, t[x].son[qwq ^ 1] = f;
    pushup(f), pushup(x);
} 
int st[N];
void Splay( int x ) {
    int top = 0, now = x; st[++top] = now;
    while( !isroot(now) ) st[++top] = ( now = t[now].fa );
    while( top ) pushmark( st[top--] );
    while( !isroot(x) ) {
        int f = t[x].fa, ff = t[f].fa;
        if( !isroot(f) ) ( ( rs(ff) == f ) ^ ( rs(f) == x ) ) ? rotate(x) : rotate(f);
        rotate(x);
    }
}
void access( int x ) {
    for( int y = 0; x; y = x, x = t[y].fa )
        Splay( x ), t[x].son[1] = y, pushup( x );
}
void makeroot( int x ) {
    access( x ), Splay( x ), t[x].mark ^= 1, pushmark( x );
}
int findroot( int x ) {
    access( x ), Splay( x ), pushmark( x );
    while( ls(x) ) pushmark( x = ls(x) );
    return x;
}
void split( int x, int y ) {
    makeroot(x), access(y), Splay(y);
}
bool check( int x, int y ) { //判断两个点是否联通
    makeroot( x );
    return findroot( y ) != x; 
}
void link( int x, int y ) {//link的前提是这两个点联通,所以没有判断
    makeroot( x );
    t[x].fa = y;
}
signed main()
{
    n = read(), m = read();
    Idnet = n; //Idnet表示当前边

    int x, y, z, now; 

    for( register int i = 1; i <= m; ++ i ) {
        x = read(), y = read(), z = read();

        ++Idnet, w[Idnet] = z; //表示编号为Idnet的边(也是LCT中的树点)的点权变成z

        if( check( x, y ) )  
            link( x, Idnet ), link( Idnet, y ), ans += z; //如果两个不在同一个联通快里面,直接连边,并更新答案
        else 
        {
            split( x, y ), //把x-y的路径先拉出来
            now = t[y].id;
            if( t[now].mx <= z ) continue; 
            ans += ( z - t[now].mx ), Splay( t[y].id ); //先把这个点旋上去
            t[ls(now)].fa = t[rs(now)].fa = 0; //子不认父,就是断边 
            link( x, Idnet ), link( Idnet, y );//再连边    
        } 
    }
    printf("%d\n", ans);
    return 0;
}

LCT还可以做LCA欸?