bzoj3669 [Noi2014]魔法森林 LCT

13 篇文章 0 订阅

Description


给定一张n个点m条边的无向图,每条边有两个权值ai和bi。问一条联通1到N的路径上最大的ai+bi最小是多少
如果无论如何小E都无法拜访到隐士,输出“-1”(不含引号)。

2<=n<=50,000
0<=m<=100,000
1<=ai ,bi<=50,000

Solution


一个脑洞就是看到最大值最小想到二分答案然后上spfa,不过复杂度比较神奇

另一个做法就是LCT。注意到这里是边权要先化为点权
因为权值有两个因此想到限定一个求另一个最小,即对a排序动态维护b的最小生成树,每次用ai+maxb更新答案

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int INF=0x3f3f3f3f;
const int N=400005;
const int E=400005;

struct treeNode {int son[2],fa,v,max,rev,is_root;} t[N];
struct edge {int x,y,a,b;} e[E];

int fa[N];

int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

void push_up(int x) {
    t[x].max=std:: max(t[x].v,std:: max(t[t[x].son[0]].max,t[t[x].son[1]].max));
}

void push_down(int x) {
    if (!x||!t[x].rev) return ;
    t[t[x].son[0]].rev^=1;
    t[t[x].son[1]].rev^=1;
    std:: swap(t[x].son[0],t[x].son[1]);
    t[x].rev=0;
}

void rotate(int x) {
    if (t[x].is_root) return ;
    int y=t[x].fa; int z=t[y].fa;
    int k=t[y].son[1]==x;
    t[y].son[k]=t[x].son[!k];
    if (t[x].son[!k]) t[t[x].son[!k]].fa=y;
    t[x].son[!k]=y;
    t[y].fa=x; t[x].fa=z;
    if (t[y].is_root) {
        t[x].is_root=1;
        t[y].is_root=0;
    } else t[z].son[t[z].son[1]==y]=x;
    push_up(y); push_up(x);
}

void remove(int x) {
    if (!t[x].is_root) remove(t[x].fa);
    push_down(x);
}

void splay(int x) {
    remove(x);
    while (!t[x].is_root) {
        int y=t[x].fa; int z=t[y].fa;
        if (!t[y].is_root) {
            if ((t[y].son[1]==x)^(t[z].son[1]==y)) rotate(x);
            rotate(y);
        }
        rotate(x);
    }
}

void access(int x) {
    int y=0;
    do {
        splay(x);
        t[t[x].son[1]].is_root=1;
        t[t[x].son[1]=y].is_root=0;
        push_up(x);
        y=x; x=t[x].fa;
    } while (x);
}

void mroot(int x) {
    access(x); splay(x); t[x].rev^=1;
}

void link(int x,int y) {
    mroot(x); t[x].fa=y;
}

void cut(int x,int y) {
    mroot(x); access(y); splay(y);
    t[x].fa=t[y].son[0]=0;
    t[x].is_root=1;
    push_up(y);
}

int get_father(int x) {
    if (!fa[x]) return x;
    return fa[x]=get_father(fa[x]);
}

int query(int x,int y) {
    mroot(x); access(y); splay(y);
    if (!t[y].max) return y;
    while (t[y].max==t[t[y].son[0]].max||t[y].max==t[t[y].son[1]].max) {
        y=t[y].son[t[t[y].son[1]].max>t[t[y].son[0]].max];
    }
    return y;
}

bool cmp(edge a,edge b) {
    return a.a<b.a;
}

int main(void) {
    int n=read(),m=read();
    rep(i,1,m) e[i]=(edge) {read(),read(),read(),read()};
    std:: sort(e+1,e+m+1,cmp);
    rep(i,1,m) t[i+n].v=t[i+n].max=e[i].b;
    rep(i,1,n+m) t[i].is_root=1;
    int ans=INF;
    rep(i,1,m) {
        int fx=get_father(e[i].x);
        int fy=get_father(e[i].y);
        if (fx!=fy) {
            link(e[i].x,i+n);
            link(e[i].y,i+n);
            fa[fx]=fy;
        } else {
            int pos=query(e[i].x,e[i].y);
            if (e[i].b<t[pos].max) {
                cut(e[pos-n].x,pos);
                cut(e[pos-n].y,pos);
                link(e[i].x,i+n);
                link(e[i].y,i+n);
            }
        }
        if (get_father(1)==get_father(n)) {
            ans=std:: min(ans,e[i].a+t[query(1,n)].v);
        }
    }
    if (ans==INF) ans=-1;
    printf("%d\n", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值