题解:
正解是可持久化并查集
但这个显然是lct可以维护的
但这常数是个问题啊???
#include <bits/stdc++.h> using namespace std; struct re{ int a,b,c; }; const int N=5e5; int fa[N],ls[N],rs[N],v[N]; int cnt,last,last1,last2,n,m,ans; bool rev[N]; deque<re> q1,q2; void down(int x) { if (!rev[x]) return; swap(ls[x],rs[x]); rev[ls[x]]^=1; rev[rs[x]]^=1; rev[x]=0; } bool pd(int x) { if (ls[fa[x]]!=x&&rs[fa[x]]!=x) return(0); else return (1); } void rotate(int x,int y) { int fath=fa[x]; if (y==1) { rs[fath]=ls[x]; if (ls[x]) fa[ls[x]]=fath; } else { ls[fath]=rs[x]; if (rs[x]) fa[rs[x]]=fath; } fa[x]=fa[fath]; if (pd(fath)) { if (ls[fa[x]]==fath) ls[fa[x]]=x; else rs[fa[x]]=x; } fa[fath]=x; if (y==1) ls[x]=fath; else rs[x]=fath; } void dfs(int x) { if (pd(x)) dfs(fa[x]); down(x); } void splay(int x) { dfs(x); int fath=fa[x]; while (pd(x)) { if (!pd(fa[x])) { if (x==ls[fa[x]]) rotate(x,2); else rotate(x,1); } else { if (ls[fa[fath]]==fath) if (ls[fath]==x) rotate(fath,2),rotate(x,2); else rotate(x,1),rotate(x,2); else if (rs[fath]==x) rotate(fath,1),rotate(x,1); else rotate(x,2),rotate(x,1); } fath=fa[x]; } } void access(int x) { for (int y=0;x;y=x,x=fa[x]) { splay(x); rs[x]=y; } } void makeroot(int x) { access(x); splay(x); rev[x]^=1; } int findroot(int x) { access(x); splay(x); while (ls[x]) x=ls[x]; return x; } bool find (int x,int y) { makeroot(x); if (findroot(y)==x) return 1; else return 0; } void link(int x,int y) { //** //cout<<x<<" "<<y<<endl; //** makeroot(x); fa[x]=y; } void cut(int x,int y) { makeroot(x); access(y); splay(y); ls[y]=fa[x]=0; } char c[100]; int main() { freopen("noip.in","r",stdin); freopen("noip.out","w",stdout); std::ios::sync_with_stdio(false); cin>>n>>m; for (int i=1;i<=m;i++) { cin>>c;int x,y; if (c[0]=='A') { cin>>x>>y; re a; a.a=x; a.b=y; a.c=i; q1.push_back(a); if (!find(x,y)) { cnt++; ans+=i; link(x,i+n); link(i+n,y); v[i+n]=i; } last=1; last1=x; last2=y; } if (c[0]=='D') { last=2; cin>>x; q2.clear(); for (int i=1;i<=x;i++) { re y=q1.back(); q1.pop_back(); q2.push_back(y); if (find(y.a,y.c+n)) { cut(y.a,y.c+n); cut(y.b,y.c+n); cnt--; ans-=y.c; } } } if (c[0]=='R') { if (last==1) { if (find(last1,i-1+n)) { cut(last1,i-1+n); cut(last2,i-1+n); cnt--; ans-=i-1; } q1.pop_back(); } else { while (!q2.empty()) { re x=q2.back(); q2.pop_back(); q1.push_back(x); if (!find(x.a,x.b)) { cnt++; ans+=x.c; link(x.a,x.c+n); link(x.b,x.c+n); } } } } if (cnt==n-1) cout<<ans; else cout<<0; cout<<endl; } return 0; }