[BZOJ2594][WC2006]水管局长数据加强版(LCT维护最小生成树)

本文介绍了一种使用LCT(link-cut tree)处理带有边权的最小生成树问题的方法,通过将边视为点来解决传统LCT只能处理点权的问题,并提供了一段完整的C++实现代码。

题目:

我是超链接

题意:

给m条边每次操作会:1、询问x,y间一条路径,路径上的最大值最小;2、删除一条边,保证时刻至少是一棵树

题解:

似乎做过类似的题目,当时是倒序加边。这道题我们也用倒序加边,每次维护一棵最小生成树,显然最小生成树上的边满足最大值最小,动态维护最小生成树,LCT吧
但LCT似乎只能处理链上最大点权而无法保存边权。怎么办呢?我们可以考虑把边看成点 ,加一条边u-v,编号为id,则 link(u, id); link(v, id); ;删边同理。
还有一个问题,我们怎么找到这个最大的边(点)呢?其实在update的时候,我们不记录最大值,而是记录最大值的点就行了
用map的地方主要是在删边的时候判断哪条边被删掉,并记录被删掉边的编号,这样才可以实现倒序加边

代码:

#include <map>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=100005;const int M=1000005;
struct hh{int x,y,z;}po[M],ask[N];
struct line{int x,y;line(int X,int Y){x=X;y=Y;}};
map<line,int> id; map<line,bool> mp;
int maxx[N+M],val[N+M],ch[N+M][2],fa[N+M],f[N+M],delta[N+M],stack[N+M],ans[N];
bool operator <(const line a,const line b){return a.x<b.x || (a.x==b.x && a.y<b.y);}
int cmp(hh a,hh b){return a.z<b.z;}
int read()
{
    char ch=getchar(); int x=0;
    while (ch>'9' || ch<'0') ch=getchar();
    while (ch<='9' && ch>='0') x=x*10+ch-'0',ch=getchar();
    return x;
}
void updata(int now)
{
    maxx[now]=now;
    if (maxx[ch[now][0]] && val[maxx[ch[now][0]]]>val[maxx[now]]) maxx[now]=maxx[ch[now][0]];
    if (maxx[ch[now][1]] && val[maxx[ch[now][1]]]>val[maxx[now]]) maxx[now]=maxx[ch[now][1]];
}
bool Is_root(int x){return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;}
int get(int x){return ch[fa[x]][1]==x;}
void pushdown(int now)
{
    if (delta[now])
    {
        delta[ch[now][0]]^=1; delta[ch[now][1]]^=1;
        swap(ch[now][0],ch[now][1]);
        delta[now]=0;
    }
}
void rotate(int x)
{
    int old=fa[x],oldf=fa[old],which=get(x);
    fa[x]=oldf; if (!Is_root(old)) ch[oldf][get(old)]=x;
    ch[old][which]=ch[x][which^1]; fa[ch[x][which^1]]=old;
    ch[x][which^1]=old; fa[old]=x;
    updata(old); updata(x); 
}
void splay(int x)
{
    int top=0,i;
    for (i=x;!Is_root(i);i=fa[i]) stack[++top]=i;
    stack[++top]=i;
    for (int i=top;i>=1;i--) pushdown(stack[i]);
    for (;!Is_root(x);rotate(x))
      if (!Is_root(fa[x])) rotate(get(x)==get(fa[x])?fa[x]:x);
}
void access(int x)
{
    int t=0;
    for (;x;t=x,x=fa[x])
    {
        splay(x); 
        ch[x][1]=t;
        updata(x);
    }
}
void reverse(int x){access(x); splay(x); delta[x]^=1;}
void Link(int x,int y){reverse(x);fa[x]=y;splay(x);}
void Cut(int x,int y)
{
    reverse(x); access(y);
    splay(y);
    ch[y][0]=fa[x]=0;
}
int qurry(int x,int y)
{
    reverse(x); access(y);
    splay(y); return maxx[y];
}
int find(int x)
{
    if (f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
int main()
{
    int n,m,q,x,y,idd,cnt=0;n=read();m=read();q=read();
    for (int i=1;i<=m;i++) 
    {
        po[i].x=read(); po[i].y=read(); po[i].z=read();
        if (po[i].x>po[i].y) swap(po[i].x,po[i].y);
    }
    sort(po+1,po+m+1,cmp);
    for (int i=1;i<=m;i++) 
    {
        id[line(po[i].x,po[i].y)]=i+n;
        val[i+n]=po[i].z;
    }
    for (int i=1;i<=q;i++)
    {
        idd=read(); x=read(); y=read();if (x>y) swap(x,y);
        ask[i].x=x;ask[i].y=y;
        if (idd==1) ask[i].z=1; 
        else mp[line(x,y)]=1,ask[i].z=2; 
    }
    for (int i=1;i<=n;i++) f[i]=i;
    for (int i=1;i<=m;i++)
      if (!mp[line(po[i].x,po[i].y)]) 
      {
        int x=find(po[i].x),y=find(po[i].y);
        if (x!=y)
        {
            cnt++;
            Link(po[i].x,i+n);
            Link(po[i].y,i+n);
            f[x]=y;
            if(cnt==n-1) break;
        }
      }
    for (int i=q;i>=1;i--)
    {
        if (ask[i].z==1) ans[i]=val[qurry(ask[i].x,ask[i].y)];
        else
        {
            int be=qurry(ask[i].x,ask[i].y);
            if (val[be]>val[id[line(ask[i].x,ask[i].y)]])
            {
                Cut(po[be-n].x,be);
                Cut(po[be-n].y,be);
                Link(ask[i].x,id[line(ask[i].x,ask[i].y)]);
                Link(ask[i].y,id[line(ask[i].x,ask[i].y)]);
            }
        }
    }
    for (int i=1;i<=q;i++) if (ask[i].z==1) printf("%d\n",ans[i]);
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值