bzoj2959: 长跑 LCT+并查集+边双联通
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=2959
题解
调了半天,终于调完了。
显然题目要求是求出目前从 \(A\) 到 \(B\) 的可以经过重复的点(权值只算一次)的最长路。
考虑把无向图边双缩点以后,形成一棵树的关系。可以发现,边双内的点的点权是可以都取到的。所以边双缩点以后直接求出树上两个点之间的点权之和就可以了。
但是考虑怎么维护这个边双。
新链接一条边的时候,如果两个端点不连通,那么直接连上就可以了。
如果两个端点联通,那么路径上的所有点都会进入同一个边双。可以发现每个点只会进入一次边双,所以可以暴力取出这些点加入边双。
用 LCT 维护就可以了。
说起来可真轻松呢。
但是写起来的细节好多啊:
- 对于求出一个点属于哪个边双,可以用一个并查集来维护。这样的话,LCT 上每个点的父亲啊,孩子啊都会受到影响,所以只要用了父亲孩子之类的东西都要在并查集里面跑一遍。一定要注意有没有哪里没有跑并查集。
- 判断连通性的时候用 LCT 自带的找根操作太慢了,请用并查集。
- 这样下来有了两个并查集呢,不要搞混了。
- pushdown!!! pushdown 一定不能漏!!
时间复杂度只有 LCT,所以是 \(o(m\log 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\n", 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\n", 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;
}
bzoj2959: 长跑 LCT+并查集+边双联通的更多相关文章
- BZOJ2959长跑——LCT+并查集(LCT动态维护边双连通分量)
		题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前. 为 ... 
- bzoj2959: 长跑(LCT+并查集)
		题解 动态树Link-cut tree(LCT)总结 LCT常数大得真实 没有环,就是\(lct\)裸题吧 有环,我们就可以绕环转一圈,缩点 怎么搞? 当形成环时,把所有点的值全部加到一个点上,用并查 ... 
- 【bzoj2959】长跑  LCT+并查集
		题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ... 
- BZOJ 2959 长跑 (LCT+并查集)
		题面:BZOJ传送门 当成有向边做的发现过不去样例,改成无向边就忘了原来的思路.. 因为成环的点一定都能取到,我们把它们压成一个新点,权值为环上所有点的权值和 这样保证了图是一颗森林 每次询问转化为, ... 
- 【bzoj4998】星球联盟  LCT+并查集
		题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ... 
- 【BZOJ2959】长跑  (LCT+并查集)
		Time Limit: 1000 ms Memory Limit: 256 MB Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室 ... 
- BZOJ4998星球联盟——LCT+并查集(LCT动态维护边双连通分量)
		题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成 联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两 ... 
- bzoj 2959: 长跑【LCT+并查集】
		如果没有环的话直接LCT 考虑有环怎么办,如果是静态的话就tarjan了,但是这里要动态的缩环 具体是link操作的时候看一下是否成环(两点已联通),成环的话就用并查集把这条链缩到一个点,把权值加给祖 ... 
- BZOJ 2959: 长跑 LCT_并查集_点双
		真tm恶心...... Code: #include<bits/stdc++.h> #define maxn 1000000 using namespace std; void setIO ... 
随机推荐
- Nor Flash芯片特性分析
			Nor Flash是Intel在1988年推出的非易失闪存芯片,可随机读取,擦写时间长,可以擦写1~100W次,支持XIP(eXecute In Place). 本文以JS28F512M29EWH为例 ... 
- 三十八、python中反射介绍
			一.反射:根据字符串的形式去对象(某个模块)中去操作成员通过字符串的形式,导入模块通过字符串的形式,去模块中寻找指定的函数,并执行 1.__import__:用于字符串的形似执行导入模块 inp=in ... 
- Python Module_oslo.vmware_连接 vCenter
			目录 目录 前言 Install oslsvmware How to use the vSphere Web Service SDK 前言 oslo.vmware 是一个由 Python 实现的 vC ... 
- Python Module_openpyxl_styles 样式处理
			目录 目录 前言 系统软件 Working with styles Styles can be applied to the following aspects Styles模块 Copying st ... 
- 记一次Python   pip安装失败的总结
			pip 安装失败时,可能换此方法可解决1.升级pip版本,这个一般会主动提示python3 -m pip install --upgrade pip2.修改pip源,默认的pip源速度实在无法忍受,或 ... 
- JMeter常用的4种参数化方式-操作解析
			目录结构 一.JMeter参数化简介 1.JMeter参数化的概念 2.JMeter参数化方式之使用场景对比 二.JMeter参数化的4种主要方式-操作演练 1.User Parameters(用户参 ... 
- kafka producer发送消息 Failed to update metadata after问题
			提示示例: ERROR Error when sending message to topic test with key: null, value: 2 bytes with error: Fail ... 
- 记:第一次更新服务器CUDA和GPU驱动
			因有需求需要改动centos7中的CUDA(更新到10)和GUP 的driver(更新到410)的版本. 事先需要查看原版本的信息,使用nvidia-smi可以查看driver的版本信息(最新的也显示 ... 
- urllib库认证,代理,cookie
			认证,代理,cookie 1from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, buil ... 
- [转帖]深度解析区块链POW和POS的区别
			深度解析区块链POW和POS的区别 Proof of Work 还有Proof of Stake 之前理解程了 state ... 股权的意思 还有 delegated proof of Stake ... 
