SP16549 QTREE6 - Query on a tree VI(LCT 维护树上同色点连通块 + LCT维护虚子树信息)

在这里插入图片描述


答案就是 u 所在的节点同色的连通块的节点个数。

考虑用 LCT 维护同色的连通块,一种做法是:开一棵LCT,对整张图,只保留两端颜色相同的边,LCT 上每个节点维护子树节点个数。这个做法在每次修改的时候,要暴力枚举这个点的所有连边进行修改,碰到菊花图就挂了。

考虑另外一种维护同色连通块的模型:先将树转成有根树来做,在有根树上,每个点只有一条边连向其父亲,把点的颜色放到边上,并开两棵 LCT,若 c o l [ u ] = 0 col[u] = 0 col[u]=0 ,则在第 0 棵 LCT 上连 u , f a [ u ] u,fa[u] u,fa[u] 这条边。这种模型修改的时候只要修改一条边,询问时只要将顶端剪掉,输出这段链的子树节点个数,为了方便处理 1号点,为1号点连一个虚父亲。

(因为一行代码顺序的问题,调了两个多小时,真实)


代码:

#include<bits/stdc++.h>
using namespace std;
#define R register int
#define I inline void
const int maxn = 1e6 + 10;
const int N = 1e6 + 5;
typedef long long ll;
int n,m,cnt;

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 Graph {
	int head[maxn],nxt[maxn << 1],to[maxn << 1],cnt;
	void init() {
		memset(head,-1,sizeof head);
		cnt = 0;
	}
	void add(int u,int v) {
		to[cnt] = v;
		nxt[cnt] = head[u];
		head[u] = cnt++;
		to[cnt] = u;
		nxt[cnt] = head[v];
		head[v] = cnt++;
	}
}G;
int col[maxn],p[maxn];
struct LCT {						//用splay维护原森林的连通,用到了splay的操作以及数组 
	#define ls ch[x][0]
	#define rs ch[x][1]
	#define inf 0x3f3f3f3f
	int ch[maxn][2];				//ch[u][0] 表示 左二子,ch[u][1] 表示右儿子
	int f[maxn];					//当前节点的父节点 
	int tag[maxn];					//翻转标记,乘标记,加标记 
	int top,sta[maxn],siz[maxn],son[maxn];
	inline void init() {
		for (int i = 1; i <= maxn - 10; i++)
			siz[i] = 1, son[i] = 0;
	}
	inline bool get(int x) {
    	return ch[f[x]][1] == x;
	}
	inline void pushup(int x) {
		siz[x] = son[x] + 1 + siz[ls] + siz[rs];
	}
	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 的根
    	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);
			if (ch[x][1]) son[x] += siz[ch[x][1]];
			if (lst) son[x] -= siz[lst];
			ch[x][1] = lst;
			pushup(x);
			lst = x; x = f[x];
		}
	}
	inline int findroot(int x) {
		access(x); 
		splay(x); 
		int rt = x;
		while(ch[rt][0]) rt = ch[rt][0];
		return rt;
	}
	inline void link(int x) {					//连接 x 和 原树上父节点 y 的边 
		splay(x); int y = p[x];
		access(y); splay(y);
		f[x] = y; 
		son[y] += siz[x];
		siz[y] += siz[x];
	}
	inline void cut(int x) {						//cut 掉原树上 x 和 父节点的边 
		access(x); splay(x);
		f[ch[x][0]] = 0;
		ch[x][0] = 0;
		pushup(x);
	}
}tree[2];
int op,id,x,y,u,v,w;
void dfs(int u,int fa) {
	p[u] = fa;
	for (int i = G.head[u]; i + 1; i = G.nxt[i]) {
		int v = G.to[i];
		if (v == fa) continue;
		dfs(v,u);
		tree[0].link(v);
	}
}
int main() {
	n = read();
	G.init(); tree[0].init(); tree[1].init();
	memset(col,0,sizeof col);
	for (int i = 1; i < n; i++) {
		u = read(); v = read();
		G.add(u,v);
	} 
	dfs(1,n + 1);
	tree[0].link(1);
	m = read();
	while (m--) {
		op = read(); u = read();
		if (op == 0) {
			int c = col[u];
			int t = tree[c].findroot(u);
			tree[c].splay(t);
			printf("%d\n",tree[c].siz[tree[c].ch[t][1]]);
		} else {
			tree[col[u]].cut(u);
			tree[col[u] ^= 1].link(u);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值