#274. 【清华集训2016】温暖会指引我们前行(LCT 维护最大生成树)

在这里插入图片描述


用 LCT 维护一下图,当加入的边 ( u , v ) (u,v) 已连通时,考虑用 ( u , v , t ) (u,v,t) 替换掉 u , v u,v 路径上边权最小的边来使得最终字典序尽可能大,这个过程等价于维护图的最大生成树。

对每条边 u , v u,v ,将边抽象成一个点 p p ,连边 ( u , p ) , ( v , p ) (u,p) , (v,p) ,用点代替边,维护边权变成维护点权,对每个结点维护子树中温度最小的结点以及子树长度之和。

这题的额外测试点卡常,判连通要用并查集而不用 f i n d r o o t findroot


代码:

#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,res[maxn];
pii E[maxn];
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];
	int val[maxn],mi[maxn],len[maxn],sum[maxn];
	inline bool get(int x) {
    	return ch[f[x]][1] == x;
	}
	void init() {
		for (int i = 1; i <= maxn - 10; i++)
			sz[i] = 1, mi[i] = i, val[i] = 2147483647;
	}
	inline void pushup(int rt) {
		if (rt) {
			sz[rt] = 1; mi[rt] = rt; sum[rt] = len[rt];
			int ls = ch[rt][0], rs = ch[rt][1];
			if (ls) {
				sz[rt] += sz[ls];
				sum[rt] += sum[ls];
				if (val[mi[ls]] < val[mi[rt]])
					mi[rt] = mi[ls];
			}
			if (rs) {
				sz[rt] += sz[rs];
				sum[rt] += sum[rs];
				if (val[mi[rs]] < val[mi[rt]])
					mi[rt] = mi[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);
			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); f[x] = y; splay(x);
	}
	inline void cut(int x,int y) {
		split(x,y);
		ch[y][0] = f[x] = 0;
		pushup(y);
	}
}tree;
int p[maxn];
int find(int x) {
	return x == p[x] ? x : p[x] = find(p[x]);
}
int id,x,y,u,v,k[maxn],tot,vis[maxn];
char op[10];
int main() {
	n = read(); m = read();
	tree.init();
	for (int i = 1; i <= n; i++)
		p[i] = i;
	while (m--) {
		scanf("%s",op);
		if (op[0] == 'f') {
			id = read(); u = read(); v = read(); x = read(); y = read();
			id++; u++; v++;
			if (u > v) swap(u,v);
			E[id] = pii(u,v);
			tree.val[id + n] = x;
			tree.len[id + n] = y;
			tree.sum[id + n] = y;
			tree.mi[id + n] = id + n;
			if (find(u) == find(v)) {
				tree.split(u,v);
				int p = tree.mi[v] - n;
				if (tree.val[p + n] < x) {
					tree.cut(E[p].fir,p + n);
					tree.cut(E[p].sec,p + n);

					tree.link(u,id + n);
					tree.link(v,id + n);
				}
			} else {
				tree.link(u,id + n);
				tree.link(v,id + n);	
			}	
			p[find(u)] = find(v);
		} else if (op[0] == 'm') {
			u = read(); v = read();
			u++; v++;
			if (find(u) != find(v)) puts("-1");
			else {
				tree.move_to_root(u);
				tree.access(v);
				tree.splay(v);
				printf("%d\n",tree.sum[v]);
			}
		} else {
			id = read(); x = read();
			id++;
			tree.splay(id + n);
			tree.sum[id + n] += x - tree.len[id + n];
			tree.len[id + n] = x;
		}
	}
	return 0;
}
发布了332 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

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