标签:不可 BMI space 假设 nio upd bzoj 使用 str
时间倒流
首先把所有的操作全都读进来
然后把最终形态的树建出来,
这样删边操作就变成了加边操作
维护联通性用LCT,缩点的话用并查集维护祖先,然后暴力改祖先即可
access操作稍微有一些改动,具体看代码吧
// luogu-judger-enable-o2 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN = 1e5+10; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < ‘0‘ || c > ‘9‘) {if(c == ‘-‘) f = -1; c = getchar();} while(c >= ‘0‘ && c <= ‘9‘) {x = x * 10 + c - ‘0‘; c = getchar();} return x * f; } int N, M; int father[MAXN]; int find(int x) { if(father[x] == x) return father[x]; else return father[x] = find(father[x]); } int unionn(int x, int y) { int fx = find(x), fy = find(y); father[fx] = fy; } #define ls(x) T[x].ch[0] #define rs(x) T[x].ch[1] #define fa(x) T[x].f struct node { int f, ch[2], r, siz; }T[MAXN]; int ident(int x) { return T[fa(x)].ch[0] == x ? 0 : 1; } void update(int x) { T[x].siz = T[ls(x)].siz + T[rs(x)].siz + 1; } void pushdown(int x) { if(T[x].r) { T[ls(x)].r ^= 1, T[rs(x)].r ^= 1; swap(ls(x), rs(x)); T[x].r = 0; } } void connect(int x, int fa, int how) { T[x].f = fa; T[fa].ch[how] = x; } bool IsRoot(int x) { return T[fa(x)].ch[0] != x && T[fa(x)].ch[1] != x; } void rotate(int x) { int Y = fa(x), R = fa(Y), Yson = ident(x), Rson = ident(Y); int B = T[x].ch[Yson ^ 1]; T[x].f = R; if(!IsRoot(Y)) connect(x, R, Rson); connect(B, Y, Yson); connect(Y, x, Yson ^ 1); update(Y);update(x); } int st[MAXN]; void splay(int x) { int y = x, top = 0; st[++top] = y; while(!IsRoot(y)) st[++top] = y = fa(y); while(top) pushdown(st[top--]); for(int y = fa(x); !IsRoot(x); rotate(x), y = fa(x)) if(!IsRoot(y)) rotate( ident(x) == ident(y) ? y : x); } void access(int x) { for(int y = 0; x;y = x, x = fa(y) = find(fa(x)))//这里要改一下 splay(x), rs(x) = y, update(x); } void makeroot(int x) { access(x); splay(x); T[x].r ^= 1; } void link(int x, int y) { //makeroot(x); T[x].f = y; } int findroot(int x) { //makeroot(x); access(x);splay(x); pushdown(x); while(ls(x)) pushdown(x = ls(x)); splay(x); return x; } void delet(int x, int to) { if(x) father[x] = to, delet(ls(x), to), delet(rs(x), to); } void merge(int x, int y) { if(x == y) return ; makeroot(x); if(findroot(y) != x) { link(x, y); return ; } delet(rs(x), x); rs(x) = 0; update(x); } int split(int x,int y) { makeroot(x); access(y); splay(y); } struct Edge { int u,v; bool operator < (const Edge &rhs) const{ return u < rhs.u || (u == rhs.u && v < rhs.v); } }E[MAXN]; int vis[MAXN]; struct Query { int opt, x, y; }Q[MAXN]; int Qnum = 0; int ans[MAXN]; int main() { #ifdef WIN32 freopen("a.in", "r", stdin); #else //freopen("lane.in","r",stdin); //freopen("lane.out","w",stdout); #endif N = read(), M = read(); for(int i = 1; i <= N; i++) father[i] = i; for(int i = 1; i <= M; i++) { E[i].u = read(), E[i].v = read(); if(E[i].u > E[i].v) swap(E[i].u, E[i].v); } sort(E + 1, E + M + 1); while(scanf("%d", &Q[++Qnum].opt) && Q[Qnum].opt != -1) { Q[Qnum].x = read(), Q[Qnum].y = read(); if(Q[Qnum].x > Q[Qnum].y) swap(Q[Qnum].x, Q[Qnum].y); if(Q[Qnum].opt == 0) vis[lower_bound(E + 1, E + M + 1, (Edge){Q[Qnum].x, Q[Qnum].y} ) - E] = 1; } Qnum--; for(int i = 1; i <= M; i++) if(!vis[i]) merge( find(E[i].u), find(E[i].v) ); int ansnum = 0; for(int i = Qnum; i >= 1; i--) { Q[i].x = find(Q[i].x); Q[i].y = find(Q[i].y); if(Q[i].opt == 0) merge(Q[i].x, Q[i].y); else split(Q[i].x, Q[i].y), ans[++ansnum] = T[Q[i].y].siz - 1; } while(ansnum) printf("%d\n", ans[ansnum--]); return 0; }
BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)
标签:不可 BMI space 假设 nio upd bzoj 使用 str
原文地址:https://www.cnblogs.com/zwfymqz/p/8972879.html