bzoj4736 温暖会指引我们前行(lct)

lct维护最大生成树即可。
好久没打打着好别扭的感觉qaq
药丸qaq

#include 
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 400010
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,fa[N],c[N][2],pa[N>>2],mn[N],sum[N],v[N],w[N],ux[N],vx[N],q[N];
char op[10];bool ex[N],rev[N];
inline int find1(int x){return pa[x]==x?x:pa[x]=find1(pa[x]);}
inline bool isroot(int x){return x!=c[fa[x]][0]&&x!=c[fa[x]][1];}
inline void pushdown(int x){
    if(!rev[x]) return;rev[x]=0;rev[c[x][0]]^=1;rev[c[x][1]]^=1;
    swap(c[x][0],c[x][1]);
}
inline int Min(int x,int y){return v[x]inline void update(int x){
    int l=c[x][0],r=c[x][1];mn[x]=x;sum[x]=w[x];
    mn[x]=Min(mn[x],mn[l]);sum[x]+=sum[l];
    mn[x]=Min(mn[x],mn[r]);sum[x]+=sum[r];
}
inline void rotate(int x){
    int y=fa[x],z=fa[y],l=x==c[y][1],r=l^1;
    if(!isroot(y)) c[z][y==c[z][1]]=x;
    fa[c[x][r]]=y;fa[y]=x;fa[x]=z;
    c[y][l]=c[x][r];c[x][r]=y;update(y);update(x);
}
inline void splay(int x){
    int top=0;q[++top]=x;
    for(int xx=x;!isroot(xx);xx=fa[xx]) q[++top]=fa[xx];
    while(top) pushdown(q[top--]);
    while(!isroot(x)){
        int y=fa[x],z=fa[y];
        if(!isroot(y)){
            if(x==c[y][1]^y==c[z][1]) rotate(x);
            else rotate(y);
        }rotate(x);
    }
}
inline void access(int x){
    int y=0;while(x){splay(x);c[x][1]=y;update(x);y=x;x=fa[x];}
}
inline void makeroot(int x){
    access(x);splay(x);rev[x]^=1;
}
inline void link(int x,int y){
    makeroot(x);fa[x]=y;
}
inline void cut(int x,int y){
    makeroot(x);access(y);splay(y);fa[x]=c[y][0]=0;update(y);
}
inline int qmn(int x,int y){
    makeroot(x);access(y);splay(y);return mn[y];
}
inline int qsum(int x,int y){
    makeroot(x);access(y);splay(y);return sum[y];
}
inline void change(int x,int y){
    splay(x);w[x]=y;update(x);
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();
    for(int i=0;i<=n;++i) pa[i]=i,v[i]=inf,w[i]=0;
    while(m--){
        scanf("%s",op+1);
        if(op[1]=='f'){
            int id=read()+n+1,x=read()+1,y=read()+1;
            v[id]=read();w[id]=read();ux[id]=x;vx[id]=y;
            if(find1(x)!=find1(y)){
                link(x,id),link(id,y),pa[pa[x]]=pa[y];
                ex[id]=1;continue;
            }int z=qmn(x,y);if(v[z]>v[id]) continue;ex[z]=0;ex[id]=1;
            cut(z,vx[z]);cut(z,ux[z]);link(x,id);link(id,y);continue;
        }if(op[1]=='m'){
            int x=read()+1,y=read()+1;
            if(find1(x)!=find1(y)) puts("-1");
            else printf("%d\n",qsum(x,y));continue;
        }int id=read()+n+1,x=read();if(ex[id]) change(id,x); 
    }return 0;
}

你可能感兴趣的:(bzoj,link,cut,tree)