Luogu4234:最小差值生成树

题面

luogu

Sol

好久没写 LCT L C T
然而写跪了 TAT T A T

把边从小到大加入森林
如果形成环,就替换最小的边
如果已经是树,更新答案
LCT L C T 维护

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(4e5 + 5);

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, fa[_], ch[2][_], mn[_], val[_], rev[_], ans, S[_], bt[_], vis[_];
struct Edge{
    int u, v, w;

    IL bool operator <(RG Edge B) const{
        return w < B.w;
    }
} edge[_];

IL int Son(RG int x){
    return ch[1][fa[x]] == x;
}

IL int Isroot(RG int x){
    return ch[0][fa[x]] != x && ch[1][fa[x]] != x;
}

IL int Chk(RG int x, RG int y){
    return val[x] < val[y] ? x : y;
}

IL void Update(RG int x){
    mn[x] = Chk(x, Chk(mn[ch[0][x]], mn[ch[1][x]]));
}

IL void Reverse(RG int x){
    if(!x) return;
    rev[x] ^= 1, swap(ch[0][x], ch[1][x]);
}

IL void Pushdown(RG int x){
    if(!rev[x]) return;
    rev[x] = 0, Reverse(ch[0][x]), Reverse(ch[1][x]);
}

IL void Rotate(RG int x){
    RG int y = fa[x], z = fa[y], c = Son(x);
    if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
    ch[c][y] = ch[!c][x], fa[ch[c][y]] = y;
    ch[!c][x] = y, fa[y] = x, Update(y);
}

IL void Splay(RG int x){
    S[S[0] = 1] = x;
    for(RG int y = x; !Isroot(y); y = fa[y]) S[++S[0]] = fa[y];
    while(S[0]) Pushdown(S[S[0]--]);
    for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
        if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
    Update(x);
}

IL void Access(RG int x){
    for(RG int y = 0; x; y = x, x = fa[x]) Splay(x), ch[1][x] = y, Update(x);
}

IL void Makeroot(RG int x){
    Access(x), Splay(x), Reverse(x);
}

IL void Link(RG int x, RG int y){
    Makeroot(x), fa[x] = y;
}

IL void Split(RG int x, RG int y){
    Makeroot(x), Access(y), Splay(y);
}

IL void Cut(RG int x, RG int y){
    Split(x, y), ch[0][y] = fa[x] = 0, Update(y);
}

IL int Find(RG int x){
    return bt[x] == x ? x : bt[x] = Find(bt[x]);
}

int main(RG int argc, RG char *argv[]){
    n = Input(), m = Input(), ans = val[0] = 233233;
    for(RG int i = 1; i <= n + m; ++i) bt[i] = i, val[i] = 23333;
    for(RG int i = 1; i <= m; ++i){
        RG int u = Input(), v = Input(), w = Input();
        edge[i] = (Edge){u, v, w};
    }
    sort(edge + 1, edge + m + 1);
    for(RG int i = 1, tot = 0, t = 1; i <= m; ++i){
        RG int x = Find(edge[i].u), y = Find(edge[i].v);
        val[i + n] = edge[i].w;
        if(x == y){
            if(edge[i].u == edge[i].v) continue;
            Split(edge[i].u, edge[i].v);
            RG int pos = mn[edge[i].v];
            Cut(edge[pos - n].u, pos), Cut(edge[pos - n].v, pos);
            Link(edge[i].u, i + n), Link(edge[i].v, i + n);
            vis[i] = 1, vis[pos - n] = 0;
        }
        else{
            vis[i] = 1, bt[x] = y, ++tot;
            Link(edge[i].u, i + n), Link(edge[i].v, i + n);
        }
        while(!vis[t]) ++t;
        if(tot == n - 1) ans = min(ans, edge[i].w - edge[t].w);
    }
    printf("%d\n", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值