BZOJ4736 温暖会指引我们前行

题目大意

给定n个点,要维护三种操作:
find id u v t l:在u,v点间连一条编号id,温度t,长度l的边(保证温度互不相同)。
move u v:询问在u到v的路径中,温度从小到大排序后字典序最大的路径的长度。若不存在路径,输出-1
change id l:将编号为id的边长度改为l

sample input
8 19
find 0 0 2 7 2
find 1 2 4 4 4
find 2 4 6 10 1
find 3 6 7 8 6
move 2 7
move 1 6
find 4 2 5 3 4
move 0 5
change 0 12
find 5 4 5 5 10
find 6 2 3 6 9
move 3 5
find 7 0 1 12 1
move 1 6
find 8 1 7 11 100
move 1 6
move 3 7
move 5 6
move 2 2
Sample out
11
-1
6
23
18
106
122
11
0

Solution

这个询问的操作看起来很奇怪,但仔细分析一下,其实就是求整幅图最大生成树在u,v之间的链(不证明了)。只要维护好这棵最大生成树就可以。
加入一条边时,如果u,v本来就不在一个联通块里(并查集维护),直接连边就可以。
如果在一个联通块里,这条新加的边可能会替换原来的边。如果新加的边权还小于u,v间链上最小的边权,就不用加边了。否则把用新加的边把最小边权的边替换掉。
而修改时,只改了长度,并没有改最重要的温度,所以没什么关系。
要加边、删边,并维护书上一条链上的最小值,用LCT维护就行了(其实不难写)。

代码
#include<stdio.h>
using namespace std;
int son[500010][2],tree[500010],a[500010],Pos[500010],fa[500010],q[500010],rev[500010];
int Fa[500010],U[500010],V[500010],n,m;
int sum[500010],L[500010];
char st[100];
inline void swap(int &x,int &y)
{
    int t=x;
    x=y,y=t;
}
inline int find(int x)
{
    if (Fa[x]==x) return x;
    Fa[x]=find(Fa[x]);
    return Fa[x];
}
inline int isroot(int x)
{
    return (son[fa[x]][0]!=x&&son[fa[x]][1]!=x);
}
inline void pushdown(int u)
{
    if (rev[u])
    {
        rev[u]^=1;
        int l=son[u][0],r=son[u][1];
        rev[l]^=1,rev[r]^=1;
        swap(son[u][0],son[u][1]);
    }
}
inline void update(int x)
{
    sum[x]=sum[son[x][0]]+sum[son[x][1]]+L[x];
    if (tree[son[x][0]]<tree[son[x][1]])
        tree[x]=tree[son[x][0]],Pos[x]=Pos[son[x][0]];
    else tree[x]=tree[son[x][1]],Pos[x]=Pos[son[x][1]];
    if (a[x]<tree[x]) tree[x]=a[x],Pos[x]=x;
}
inline void rotate(int x)
{
    int y=fa[x],z=fa[y];
    int l=(son[y][1]==x),r=l^1;
    if (!isroot(y))
        son[z][son[z][1]==y]=x;
    fa[son[x][r]]=y,fa[x]=z,fa[y]=x;
    son[y][l]=son[x][r],son[x][r]=y;
    update(y);
    update(x);
}
void splay(int x)
{
    int top=0;
    q[++top]=x;
    for (int i=x;!isroot(i);i=fa[i])
        q[++top]=fa[i];
    while (top) pushdown(q[top--]);
    while (!isroot(x))
    {
        int y=fa[x],z=fa[y];
        if (!isroot(y))
        {
            if ((son[y][0]==x)^(son[z][0]==y)) rotate(x);
            else rotate(y);
        }
        rotate(x);
    }
}
void access(int x)
{
    for (int t=0;x;t=x,x=fa[x])
    {
        splay(x);
        son[x][1]=t;
        update(x);
    }
}
void makeroot(int u)
{
    access(u);
    splay(u);
    rev[u]^=1;
}
void split(int u,int v)
{
    makeroot(u);
    access(v);
    splay(v);
}
void link(int u,int v)
{
    makeroot(u);
    fa[u]=v;
}
void cut(int u,int v)
{
    makeroot(u);
    access(v);
    splay(v);
    son[v][0]=fa[u]=0;
    update(v);
}
void add(int u,int v,int val,int pos)
{
    int x=find(u),y=find(v);
    if (x!=y)
    {
        Fa[x]=y;
        link(u,pos+n);link(pos+n,v); 
    } 
    else
    {
        split(u,v);
        if (tree[v]<val)
        {
            int VV=Pos[v]-n;
            cut(U[VV],VV+n);
            cut(VV+n,V[VV]); 
            link(u,pos+n);
            link(pos+n,v);
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    int id,u,v,t,l;
    for (int i=1;i<=n;i++) Fa[i]=i;
    for (int i=0;i<=n+m;i++) a[i]=tree[i]=1000000000;
    while (m--)
    {
        scanf("%s",st);
        if (st[0]=='f')
        {
            scanf("%d%d%d%d%d",&id,&u,&v,&t,&l);
            u++,v++;
            U[id+1]=u,V[id+1]=v;
            a[id+1+n]=t;L[id+1+n]=l;
            add(u,v,t,id+1);
        }
        else
            if (st[0]=='m')
            {
                scanf("%d%d",&u,&v);
                u++,v++;
                if (find(u)==find(v))
                {
                    split(u,v);
                    printf("%d\n",sum[v]);
                }
                else puts("-1");
            }
            else
            if (st[0]=='c')
            {
                scanf("%d%d",&id,&l);
                splay(id+1+n);
                L[id+1+n]=l;
                update(id+1+n);
            }
    } 
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值