SP16549 QTREE6 - ツリーVI(LCT + LCTメンテナンスツリーメンテナンス仮想通信ブロック情報サブツリー同じカラーポイント)にクエリ

ここに画像を挿入説明


答えがブロックの通信ノードの数は、uはここで同じ色ノードということです。

LCTとの通信を維持することは、色ブロックを考慮し、アプローチがある:LCTオープン、グラフ全体に、同じ色のエッジのみ2つの端部を残して、各ノードはサブツリーの数がLCTノード維持します。この方法は、各変更時に、暴力の列挙のポイントにすべての側面でも図がハングアップ会った菊を変更します。

カラードットと開放の縁に、第行うツリールート付きツリー、ルートツリーに変換し、その父親に接続されたそれぞれの側の一点のみ:別の考慮事項は、同じカラーモデルのブロックとの通信を維持するためにもし2つのLCT、 C インクルード リットル [ u ] = 0 COL [U] = 0 、その後も、最初のLCTの0 u , f a [ u ] F、第四の[E] このエッジ。処理ポイント1を容易にするために、トップカット、このサブツリーチェーンの出力ノードの数限り求めエッジ変更が、点1は、仮想父親に接続されるたびに、この変形モデル。

(ため、コード配列の問題ライン、曲つ以上の時間、TRUE)


コード:

#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;
}

公開された332元の記事 ウォン称賛10 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_41997978/article/details/104524694