P2173 [ZJOI2012]网络(LCT 维护染色连通块)

在这里插入图片描述


根据第三个操作给的启发,由于颜色数量不超过10种,把每一种颜色的图抽出来用 LCT 维护,就变成了 LCT 模板题,用 LCT 判连通,维护链上最小值只要维护子树最小值。

其实和染色连通块没什么关系。。。
但这题输入数据可能会出现换的边是同样的颜色,特判一下即可。


代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
typedef long long ll;
#define pii pair<int,int>
#define fir first
#define sec second
int n,m,C,k,cnt;
map<int,int> mp[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的操作以及数组 
	#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],val[maxn],mxval[maxn]; 
	inline bool get(int x) {
    	return ch[f[x]][1] == x;
	}
	void pushdown(int x) {
		if (tag[x]) {
			if (ls) {
				tag[ls] ^= 1;
				swap(ch[ls][0],ch[ls][1]);
			}
			if (rs) {
				tag[rs] ^= 1;
				swap(ch[rs][0],ch[rs][1]);
			}
			tag[x] = 0;
		}
	}
	inline void pushup(int x) {
		mxval[x] = max(mxval[ls],max(val[x],mxval[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 的根
		top = 0; sta[++top] = x;
		for (int i = x; !isroot(i); i = f[i]) sta[++top] = f[i];
		for (int i = top; i >= 1; i--) pushdown(sta[i]);
    	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) {
		access(x); splay(x); 
		tag[x] ^= 1; 
		swap(ch[x][0],ch[x][1]);
	}
	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 移到根 
	}
	inline void cut(int x,int y) {
		split(x,y);
		ch[y][0] = f[x] = 0;
		pushup(y);
	}
}tree[15];
int d[15][maxn];
int op,id,x,y,u,v,w;
int main() {
	n = read(); m = read(); C = read(); k = read();
	for (int i = 1; i <= n; i++) {
		v = read();
		for (int j = 1; j <= C; j++)
			tree[j].val[i] = v;
	}
	for (int i = 1; i <= m; i++) {
		u = read(); v = read(); w = read(); w++;
		if (u > v) swap(u,v);
		tree[w].link(u,v);
		d[w][u]++; d[w][v]++;
		mp[u][v] = w;
	}
	while (k--) {
		op = read();
		if (op == 0) {
			x = read(); y = read();
			for (int i = 1; i <= C; i++) {
				tree[i].splay(x);
				tree[i].val[x] = y;	
			}
		} else if (op == 1) {
			u = read(); v = read(); w = read(); w++;
			if (u > v) swap(u,v);
			if (w == mp[u][v]) {
				puts("Success.");
			}
			else if (mp[u][v] == 0) {
				puts("No such edge.");
			} else {
				d[mp[u][v]][u]--; d[mp[u][v]][v]--;
				if (d[w][u] == 2 || d[w][v] == 2) {
					d[mp[u][v]][u]++; d[mp[u][v]][v]++;
					puts("Error 1.");
				} else if (tree[w].findroot(u) == tree[w].findroot(v)) {
					d[mp[u][v]][u]++; d[mp[u][v]][v]++;
					puts("Error 2.");
				} else {
					tree[mp[u][v]].cut(u,v);
					d[w][u]++; d[w][v]++;
					tree[w].link(u,v);
					mp[u][v] = w;
					puts("Success.");
				}	
			}
		} else {
			w = read(); u = read(); v = read(); w++;
			if (tree[w].findroot(u) != tree[w].findroot(v)) {
				puts("-1");
			} else {
				tree[w].split(u,v);
				printf("%d\n",tree[w].mxval[v]);	
			}
		}
	}
	return 0;
}
发布了332 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

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