Code:
#include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char *p1,*p2,buf[100000]; #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++) int rd() {int x=0,f=1; char c=nc(); while(c<48) {if(c==‘-‘) f=-1; c=nc();} while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x*f;} void setIO(string s) { string in=s+".in"; string out=s+".out"; freopen(in.c_str(),"r",stdin); freopen(out.c_str(),"w",stdout); } struct Union { int p[maxn]; inline void init() { for(int i=0;i<maxn;++i) p[i]=i; } inline int find(int x) { return p[x]==x?x:p[x]=find(p[x]); } inline int merge(int x,int y) { int a=find(x),b=find(y); if(a==b) return 0; p[a]=b; return 1; } }con; struct LCT { #define lson ch[x][0] #define rson ch[x][1] #define isrt(x) (!(ch[f[x]][1]==x || ch[f[x]][0]==x)) #define get(x) (ch[f[x]][1]==x) int ch[maxn][2], maxv[maxn], rev[maxn], sumv[maxn], val[maxn], sta[maxn], idx[maxn], f[maxn]; inline void mark(int x) { if(!x)return; swap(lson,rson), rev[x]^=1; } inline void pushup(int x) { if(!x)return; idx[x]=x, maxv[x]=val[x]; if(maxv[lson] > maxv[x]) idx[x] = idx[lson], maxv[x]=maxv[lson]; if(maxv[rson] > maxv[x]) idx[x] = idx[rson], maxv[x]=maxv[rson]; sumv[x]=sumv[lson]+sumv[rson]+val[x]; } inline void pushdown(int x) { if(!x)return; if(rev[x]) mark(lson), mark(rson), rev[x]^=1; } inline void rotate(int x) { int old=f[x],fold=f[old], which=get(x); if(!isrt(old)) ch[fold][ch[fold][1]==old]=x; ch[old][which]=ch[x][which^1], f[ch[old][which]]=old; ch[x][which^1]=old, f[old]=x, f[x]=fold; pushup(old), pushup(x); } inline void splay(int x) { int u=x,v=0,fa; for(sta[++v]=u;!isrt(u);u=f[u]) sta[++v]=f[u]; while(v) pushdown(sta[v--]); for(u=f[u];(fa=f[x])!=u;rotate(x)) if(f[fa]!=u) rotate(get(fa)==get(x)?fa:x); } inline void Access(int x) { int t=0; while(x) { splay(x), rson=t, pushup(x), t=x,x=f[x]; } } inline void MakeRoot(int x) { Access(x), splay(x), mark(x); } inline void split(int x,int y) { MakeRoot(x), Access(y), splay(y); } inline void cut(int x,int y) { MakeRoot(x), Access(y), splay(y); ch[y][0]=f[x]=0; pushup(y); } inline void link(int x,int y) { MakeRoot(x), f[x]=y; } }tr; int n,m,Q,eds; int from[maxn],to[maxn], cap[maxn], answer[maxn]; map<int,int>ck[maxn]; struct OPT { int o, x,y,eds, id, ans; }opt[maxn]; int main() { int i,j; setIO("input"); con.init(); n=rd(),m=rd(),Q=rd(); for(i=1;i<=m;++i) { ++eds; from[eds]=rd(), to[eds]=rd(), cap[eds]=rd(); ck[from[eds]][to[eds]]=ck[to[eds]][from[eds]]=eds; } for(i=1;i<=Q;++i) { opt[i].o=rd(); if(opt[i].o==1) opt[i].x=rd(), opt[i].y=rd(); if(opt[i].o==2) { int a,b; a=rd(),b=rd(); opt[i].eds = ck[a][b]; ck[a][b]=ck[b][a]=0; } } for(i=1;i<=eds;++i) { if(ck[from[i]][to[i]]) { ++Q; opt[Q].o=2; opt[Q].eds=ck[from[i]][to[i]]; } } for(i=Q;i>=1;--i) { if(opt[i].o==1) { tr.split(opt[i].x,opt[i].y); opt[i].ans=tr.maxv[opt[i].y]; } if(opt[i].o==2) { int cur=opt[i].eds,c,_new; int x=from[cur], y=to[cur], cc=cap[cur],old; if(con.merge(x, y)) { _new=cur + n; tr.val[_new]=tr.maxv[_new]=tr.sumv[_new]=cap[cur]; tr.link(_new, x), tr.link(_new, y); } else { tr.split(x,y); c=tr.idx[y]; old=tr.val[c]; if(cc < old) { int a=from[c-n], b=to[c-n]; tr.cut(a, c), tr.cut(b, c); _new=cur+n; tr.val[_new]=tr.maxv[_new]=tr.sumv[_new]=cap[cur]; tr.link(_new, x), tr.link(_new, y); } } } } for(i=1;i<=Q;++i) if(opt[i].o==1) printf("%d\n",opt[i].ans); return 0; }
luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
原文:https://www.cnblogs.com/guangheli/p/11076136.html