P4219 [BJOI2014]大融合(LCT 维护辅助树上子树信息(虚子树信息))

在这里插入图片描述


答案显然是边的左边连通块的大小 * 右边连通块的大小,考虑用 LCT 动态维护树,询问时将边 ( x , y ) (x,y) 剪短,然后输出 x x 所在辅助树的大小 * y y 所在辅助树的大小。

一般用LCT维护的都是树链上的信息,而不是子树信息。要维护子树信息,就要考虑虚树边的贡献,在这题中要维护的子树信息是 子树结点个数。设 s z 2 sz2 为虚树边的子节点个数, s z sz 为子节点个数。 p u s h u p pushup s z [ r t ] = s z [ l s ] + s z [ r s ] + s z 2 [ r t ] + 1 sz[rt] = sz[ls] + sz[rs] + sz2[rt] + 1

考虑如何维护 s z 2 sz2 ,维护 s z 2 sz2 要考虑有哪些操作会改变虚边。

首先在 r o r a t e s p l a y rorate,splay 操作中,进行翻转修改的都是实边, s z 2 sz2 的信息不会变, a c c e s s access 操作会修改虚边,在修改的地方把贡献也修改:

	inline void access(int x) {					//access操作将x 到 根路径上的边修改为重边 
		int lst = 0;
		while(x > 0) {
			splay(x);
			sz2[x] += sz[ch[x][1]] - sz[lst];	//修改虚边贡献
			ch[x][1] = lst;
			pushup(x);
			lst = x; x = f[x];
		}
	}

m o v e _ t o _ r o o t f i n d r o o t s p l i t move\_to\_root,findroot,split 操作中都是调用 s p l a y a c c e s s splay,access ,不需要多余的维护操作。

l i n k link 操作中,加了一条边 ( x , y ) (x,y) 加的是虚边,要加上这条虚边的贡献,修改时要将 x , y x,y 都通过 m o v e _ t o _ r o o t move\_to\_root 操作移动到根节点,如果不移动到根节点,修改的时候所有 y y 的父节点的 s z 2 sz2 都要修改(假设 是将 x 连在 y 的下面)

	inline void link(int x,int y) {
		move_to_root(x); move_to_root(y);			//由于 sz 维护的是辅助树中子树结点个数,不再是 splay 中子树结点个数 
		f[x] = y; splay(x);							//连边操作后,若 y 不是根,则 y 的所有父亲都要更新,不如先将 y 移到根 
		sz2[y] += sz[x]; pushup(y);
	}

在 cut 操作中,cut 掉的是实边,不会影响 s z 2 sz2 ,用 p u s h u p pushup 操作维护 s z sz 即可。

一般来说维护的虚子树信息需要满足可加减性,因为在 a c c e s s access 操作会修改实边和虚边,要扣掉之前的贡献,加上新的贡献。(对于不满足加减性的信息似乎有其它科技操作)

整体代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
typedef long long ll;
#define pii pair<int,int>
#define fir first
#define sec second
int n,m,ans;
struct node {
	int u,v,a,b;
	bool operator < (const node &rhs) const {
		return a < rhs.a;
	}	
}E[maxn];
multiset<int> st;
inline int read(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
struct LCT {						//用splay维护原森林的连通,用到了splay的操作以及数组 
	int ch[maxn][2];				//ch[u][0] 表示 左二子,ch[u][1] 表示右儿子
	int f[maxn];					//当前节点的父节点 
	int tag[maxn];					//翻转标记,乘标记,加标记 
	int top,sta[maxn],sz[maxn],sz2[maxn]; 
	inline bool get(int x) {
    	return ch[f[x]][1] == x;
	}
	void init() {
		for (int i = 1; i <= n; i++)
			sz2[i] = 0, sz[i] = 1;
	}
	inline void pushup(int rt) {
		if (rt) {
			sz[rt] = sz2[rt] + 1; 
			int ls = ch[rt][0], rs = ch[rt][1];
			if (ls) {
				sz[rt] += sz[ls];
			}
			if (rs) {
				sz[rt] += sz[rs];
			}
		}
	}
	inline void pushdown(int rt) {
		if (tag[rt]) {
			int ls = ch[rt][0], rs = ch[rt][1];
			if (ls) swap(ch[ls][0],ch[ls][1]), tag[ls] ^= 1;
			if (rs) swap(ch[rs][0],ch[rs][1]), tag[rs] ^= 1;
			tag[rt] = 0;
		}
	}
	inline bool isroot(int x) {
		return (ch[f[x]][0] != x) && (ch[f[x]][1] != x);
	}
 	inline void rotate(int x) {							//旋转操作,根据 x 在 f[x] 的哪一侧进行左旋和右旋 
	    int old = f[x], oldf = f[old];
		int whichx = get(x);
		if(!isroot(old)) ch[oldf][ch[oldf][1] == old] = x;		//如果 old 不是根节点,就要修改 oldf 的子节点信息
	    ch[old][whichx] = ch[x][whichx ^ 1];
	    ch[x][whichx ^ 1] = old;
	    f[ch[old][whichx]] = old;
	    f[old] = x; f[x] = oldf;
		pushup(old); pushup(x); 
	}
	inline void splay(int x) {								//将 x 旋到所在 splay 的根
		top = 0; sta[++top] = x;
		for (int i = x; !isroot(i); i = f[i]) sta[++top] = f[i]; //在 splay 中维护 下推标记 
		while(top) pushdown(sta[top--]);
    	for(int fa = f[x]; !isroot(x); rotate(x), fa = f[x]) {	//再把x翻上来
        	if(!isroot(fa))										//如果fa非根,且x 和 fa是同一侧,那么先翻转fa,否则先翻转x 
            	rotate((get(x) == get(fa)) ? fa : x);
        }
	}
	inline void access(int x) {					//access操作将x 到 根路径上的边修改为重边 
		int lst = 0;
		while(x > 0) {
			splay(x);
			sz2[x] += sz[ch[x][1]] - sz[lst];
			ch[x][1] = lst;
			pushup(x);
			lst = x; x = f[x];
		}
	}
	inline void move_to_root(int x) {			//将 x 移到 x 所在树的根(不是所在splay的根,所在splay只是一条重链) 
		access(x); splay(x); tag[x] ^= 1; swap(ch[x][0],ch[x][1]);
		//将 x 移到 根之后 x 是深度最低的点,这条重链、这棵splay上所有点的深度颠倒,
		//所有的点的左子树的点应该到右子树,因此要翻转这棵splay的左右子树
	}
	inline int findroot(int x) {
		access(x); 
		splay(x); 
		int rt = x;
		while(ch[rt][0]) rt = ch[rt][0];
		return rt;
	}
	inline void split(int x,int y) {
		move_to_root(x); access(y); splay(y);
	}
	inline void link(int x,int y) {
		move_to_root(x); move_to_root(y);			//由于 sz 维护的是辅助树中子树结点个数,不再是 splay 中子树结点个数 
		f[x] = y; splay(x);							//连边操作后,若 y 不是根,则 y 的所有父亲都要更新,不如先将 y 移到根 
		sz2[y] += sz[x]; pushup(y);
	}
	inline void cut(int x,int y) {
		split(x,y);
		ch[y][0] = f[x] = 0;
		pushup(y);
	}
}tree;
int id,x,y,u,v,k[maxn],tot,vis[maxn];
char op[10];
int main() {
	n = read(); m = read();
	tree.init();
	while (m--) {
		scanf("%s",op);
		x = read(); y = read();
		if (op[0] == 'A') {
			tree.link(x,y);
		} else {
			tree.cut(x,y);
			tree.move_to_root(x);
			tree.move_to_root(y);
			printf("%lld\n",1ll * tree.sz[x] * tree.sz[y]);
			tree.link(x,y);
		}
	}
	return 0;
}
发布了332 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41997978/article/details/104377045