• bzoj2959: 长跑 LCT+并查集+边双联通


    题目传送门

    https://lydsy.com/JudgeOnline/problem.php?id=2959

    题解

    调了半天,终于调完了。


    显然题目要求是求出目前从 (A)(B) 的可以经过重复的点(权值只算一次)的最长路。

    考虑把无向图边双缩点以后,形成一棵树的关系。可以发现,边双内的点的点权是可以都取到的。所以边双缩点以后直接求出树上两个点之间的点权之和就可以了。

    但是考虑怎么维护这个边双。

    新链接一条边的时候,如果两个端点不连通,那么直接连上就可以了。

    如果两个端点联通,那么路径上的所有点都会进入同一个边双。可以发现每个点只会进入一次边双,所以可以暴力取出这些点加入边双。

    用 LCT 维护就可以了。


    说起来可真轻松呢。

    但是写起来的细节好多啊:

    1. 对于求出一个点属于哪个边双,可以用一个并查集来维护。这样的话,LCT 上每个点的父亲啊,孩子啊都会受到影响,所以只要用了父亲孩子之类的东西都要在并查集里面跑一遍。一定要注意有没有哪里没有跑并查集。
    2. 判断连通性的时候用 LCT 自带的找根操作太慢了,请用并查集。
    3. 这样下来有了两个并查集呢,不要搞混了。
    4. pushdown!!! pushdown 一定不能漏!!

    时间复杂度只有 LCT,所以是 (o(mlog n))

    #include<bits/stdc++.h>
    
    #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
    #define fi first
    #define se second
    #define pb push_back
    
    template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
    template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
    
    typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
    
    template<typename I> inline void read(I &x) {
    	int f = 0, c;
    	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    	x = c & 15;
    	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    	f ? x = -x : 0;
    }
    
    const int N = 1.5e5 + 7;
    
    #define lc c[0]
    #define rc c[1]
    
    int n, m;
    int a[N], fa[N], fa2[N], lian[N];
    
    inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
    inline int find2(int x) { return fa2[x] == x ? x : fa2[x] = find2(fa2[x]); }
    
    inline void self_kill() { std::cerr << a[N * 100]; }
    
    struct Node { int c[2], fa, rev, s, v, sum; } t[N];
    int st[N];
    inline bool isroot(int o) { return find2(t[find2(t[o].fa)].lc) != o && find2(t[find2(t[o].fa)].rc) != o; }
    inline bool idtfy(int o) { return find2(t[find2(t[o].fa)].rc) == o; }
    inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
    inline void pushup(int o) {
    	if (find2(o) != o) dbg("o = %d, find2(o) = %d
    ", o, find2(o));
    	if (find2(o) != o) self_kill();
    	t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
    	t[o].sum = t[t[o].lc].sum + t[t[o].rc].sum + t[o].v;
    }
    inline void pushdown(int o) {
    	if (!t[o].rev) return;
    	if (t[o].lc) t[t[o].lc].rev ^= 1, std::swap(t[t[o].lc].lc, t[t[o].lc].rc);
    	if (t[o].rc) t[t[o].rc].rev ^= 1, std::swap(t[t[o].rc].lc, t[t[o].rc].rc);
    	t[o].rev ^= 1;
    }
    inline void rotate(int o) {
    	int fa = find2(t[o].fa), pa = find2(t[fa].fa), d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
    	if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
    	connect(o, fa, d1 ^ 1), connect(fa, b, d1);
    	pushup(fa), pushup(o);
    	assert(o != t[o].fa);
    }
    inline void splay(int o) {
    	if (find2(o) != o) self_kill();
    	assert(find2(o) == o);
    	int x = o, tp = 0;
    	st[++tp] = x;
    	while (!isroot(x)) st[++tp] = x = find2(t[x].fa);
    	while (tp) pushdown(st[tp--]);
    	while (!isroot(o)) {
    		int fa = find2(t[o].fa);
    		assert(o != fa);
    		if (isroot(fa)) rotate(o);
    		else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
    		else rotate(o), rotate(o);
    	}
    }
    inline void access(int o) {
    	assert(find2(o) == o);
    	for (int x = 0; o; o = find2(t[x = o].fa))
    		splay(o), t[o].rc = x, pushup(o);
    }
    inline void mkrt(int o) {
    	assert(find2(o) == o);
    	access(o), splay(o);
    	t[o].rev ^= 1, std::swap(t[o].lc, t[o].rc);
    }
    inline int getrt(int o) {
    	assert(find2(o) == o);
    	access(o), splay(o);
    	while (pushdown(o), t[o].lc) o = find2(t[o].lc);
    	return splay(o), o;
    }
    inline void link(int x, int y) {
    	assert(find2(x) == x);
    	assert(find2(y) == y);
    	mkrt(x);
    	t[x].fa = y;
    }
    inline void cut(int x, int y) {
    	assert(find2(x) == x);
    	assert(find2(y) == y);
    	mkrt(x), access(y), splay(y);
    	if (t[y].lc == x && t[x].rc == 0) t[x].fa = t[y].lc = 0, pushup(y);
    }
    
    inline void dfs(int o) {
    	if (!o) return;
    	pushdown(o);
    	dfs(find2(t[o].lc));
    	lian[++lian[0]] = o;
    	dfs(find2(t[o].rc));
    }
    
    inline void work() {
    	while (m--) {
    		int opt, x, y;
    		read(opt), read(x), read(y);
    		if (opt == 1) {
    			x = find2(x), y = find2(y);
    			if (find(x) != find(y)) link(x, y), fa[find(x)] = find(y);
    			else {
    				mkrt(x), access(y), splay(y);
    				lian[0] = 0, dfs(y);
    				for (int i = 1; i < lian[0]; ++i) fa2[lian[i]] = y, t[y].v += t[lian[i]].v;
    				assert(find2(y) == y);
    				pushup(y);
    			}
    		} else if (opt == 2) splay(find2(x)), t[find2(x)].v += y - a[x], a[x] = y, pushup(find2(x));
    		else {
    			x = find2(x), y = find2(y);
    			if (find(x) != find(y)) puts("-1");
    			else mkrt(x), access(y), splay(y), printf("%d
    ", t[y].sum);
    		}
    	}
    	puts("");
    }
    
    inline void init() {
    	read(n), read(m);
    	for (int i = 1; i <= n; ++i) read(a[i]);
    	for (int i = 1; i <= n; ++i) t[i].v = a[i], fa[i] = fa2[i] = i, pushup(i);
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	init();
    	work();
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    TQ2440之DMA+IIS
    ST公司三轴加速度计LIS3DH应用
    TQ2440之PMW
    WinForm操作SQL Server数据库笔记
    DataRelation Learning
    SQL中join,left join,right join
    C#使用异步方式调用同步方法
    .Net Framework中的委托与事件转载
    Android site link
    【java】jdk时间差问题
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj2959.html
Copyright © 2020-2023  润新知