zcmimi's blog
查看原题

点击跳转

link\ cut\ tree维护最小生成树

直接在线很难,我们可以离线加边

先把所有边(后来断掉的不算)跑一遍最小生成树

接着每次加边,设加xy,边长为w

先求出xy路径上最长的边

如果比要加的边长,则删掉这条边,加上新边

这样就可以lct维护最小生成树

因为lct不能直接维护点,所以我们可以把边看成点

x\rightarrow边\rightarrow y

#include<bits/stdc++.h>
namespace ZDY{
    #pragma GCC optimize(3)
    #define il __inline__ __attribute__ ((always_inline))
    #define rg register
    #define ll long long
    #define ull unsigned long long
    #define db double
    #define sht short
    #define MB template <class T>il
    #define Fur(i,x,y) for(int i=x;i<=y;i++)
    #define Fdr(i,x,y) for(int i=x;i>=y;i--)
    #define fl(i,x) for(int i=head[x],to;to=e[i].to,i;i=e[i].nxt)
    #define clr(x,y) memset(x,y,sizeof(x))
    #define cpy(x,y) memcpy(x,y,sizeof(x))
    #define fin(s) freopen(s".in","r",stdin)
    #define fout(s) freopen(s".out","w",stdout)
    #define fcin ios::sync_with_stdio(false)
    #define l2(n) (int(log2(n)))
    #define inf 0x3f3f3f3f
    MB T ABS(T x){return x>0?x:-x;}
    MB T MAX(T x,T y){return x>y?x:y;}
    MB T MIN(T x,T y){return x<y?x:y;}
    MB T GCD(T x,T y){return y?GCD(y,x%y):x;}
    MB void SWAP(T &x,T &y){T t=x;x=y;y=t;}
}using namespace ZDY;using namespace std;
namespace IO{const char* ln="\n";const int str=1<<20;struct IN{char buf[str],*s,*t;bool _;IN():s(buf),t(buf),_(0){}il char gc(){return s==t&&((t=(s=buf)+fread(buf,1,str,stdin))==s)?EOF:(*s++);}IN&operator>>(char&ch){if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)_=1;else ch=c;return *this;}IN& operator>>(char* ch){if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)return _=1,*this;*ch=c;ch++;while((c=gc())!=EOF&&!isspace(c))*ch=c,ch++;if(c==EOF)_=1;return *this;}IN& operator>>(string& ch){if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)return _=1,*this;ch+=c;while((c=gc())!=EOF&&!isspace(c))ch+=c;if(c==EOF)_=1;return *this;}template<typename T>IN&operator>>(T&x){if(_)return *this;char c=gc();bool ff=0;while(c!=EOF&&(c<'0'||c>'9'))ff^=(c=='-'),c=gc();if(c==EOF){_=1;return *this;}x=0;while(c!=EOF&&'0'<=c&&c<='9')x=(x<<3)+(x<<1)+c-48,c=gc();if(c==EOF)_=1;if(ff)x=-x;return *this;}}in;struct OUT{char buf[str],*s,*t;OUT():s(buf),t(buf+str){}~OUT(){fwrite(buf,1,s-buf,stdout);}void pt(char c){(s==t)?(fwrite(s=buf,1,str,stdout),*s++=c):(*s++=c);}OUT&operator<<(const char*s){while(*s)pt(*s++);return *this;}OUT&operator<<(char*s){while(*s)pt(*s++);return *this;}OUT&operator<<(string s){for(int i=0;s[i];i++)pt(s[i]);return *this;}template<typename T>OUT&operator<<(T x){if(!x)return pt('0'),*this;if(x<0)pt('-'),x=-x;char a[30],t=0;while(x)a[t++]=x%10,x/=10;while(t--)pt(a[t]+'0');return *this;}}out;}using namespace IO;
#define N 300011
#define ls c[x][0]
#define rs c[x][1]
int st[N],f[N],c[N][2],v[N],s[N];
bool rev[N];
bool irt(int x){return c[f[x]][0]!=x&&c[f[x]][1]!=x;}
void pu(int x){s[x]=MAX(MAX(s[ls],s[rs]),v[x]);}
void pr(int x){
    SWAP(ls,rs);
    rev[x]^=1;
}
void pd(int x){
    if(rev[x]){
        if(ls)pr(ls);
        if(rs)pr(rs);
        rev[x]=0;
    }
}
bool g(int x){return c[f[x]][1]==x;}
void turn(int x){
    int y=f[x],z=f[y],l=g(x),r=!l,w=c[x][r];
    if(!irt(y))c[z][g(y)]=x;
    c[x][r]=y;c[y][l]=w;
    if(w)f[w]=y;
    f[x]=z;f[y]=x;
    pu(y);pu(x);
}
void splay(int x){
    int y=x,z=0;
    st[++z]=y;
    while(!irt(y))st[++z]=y=f[y];
    while(z)pd(st[z--]);
    while(!irt(x)){
        y=f[x],z=f[y];
        if(!irt(y))
            turn(((c[y][0]==x)^(c[z][0]==y))?x:y);
        turn(x);
    }
    pu(x);
}
void access(int x){
    for(int y=0;x;x=f[y=x])
        splay(x),rs=y,pu(x);
}
void mrt(int x){
    access(x);splay(x);
    pr(x);
}
int frt(int x){
    access(x);splay(x);
    while(ls)pd(x),x=ls;
    splay(x);
    return x;
}
void sl(int x,int y){
    mrt(x);access(y);splay(y);
}
void link(int x,int y){
    mrt(x);
    if(frt(y)!=x)f[x]=y;
}
void cut(int x,int y){
    mrt(x);
    if(frt(y)==x&&f[y]==x&&!c[y][0]){
        c[x][1]=f[y]=0;
        pu(x);
    }
}
int find(int x,int k){
    if(v[x]==k)return x;
    if(s[ls]==k)return find(ls,k);
    return find(rs,k);
}
int n,m,q;
struct edge{
    edge(){b=1;}
    int x,y,w,b;
}e[N];
struct que{
    int u,x,y;
}Q[N];
int id[5011][5011],ff[N],ans[N],cnt=0;
bool cmp(edge x,edge y){
    return x.w<y.w;
}
int gf(int x){return x==ff[x]?x:(ff[x]=gf(ff[x]));}
int main(){
    fin("in");
    in>>n>>m>>q;
    int x,y,w,u,t;
    Fur(i,1,m)in>>e[i].x>>e[i].y>>e[i].w;
    sort(e+1,e+m+1,cmp);
    Fur(i,1,m){
        x=e[i].x,y=e[i].y;w=e[i].w;
        id[x][y]=id[y][x]=i;
        v[n+i]=w;
    }
    Fur(i,1,q){
        in>>u>>x>>y;
        if(u==2)e[id[x][y]].b=0;
        Q[i]=(que){u,x,y};
    }

    Fur(i,1,n)ff[i]=i;
    Fur(i,1,m)if(e[i].b){
        x=e[i].x,y=e[i].y;
        if(gf(x)!=gf(y)){
            ff[gf(x)]=gf(y);
            link(x,n+i);link(y,n+i);
        }
    }
    Fdr(i,q,1){
        u=Q[i].u;x=Q[i].x;y=Q[i].y;
        sl(x,y);
        if(u==1)ans[++cnt]=s[y];
        else
            if(s[y]>v[n+id[x][y]])
                t=find(y,s[y]),
                cut(e[t-n].x,t),
                cut(e[t-n].y,t),
                link(x,n+id[x][y]),
                link(y,n+id[x][y]);
    }
    Fdr(i,cnt,1)out<<ans[i]<<ln;
}
LG 4172 [WC2006]水管局长
comment评论
Search
search