bzoj3510 首都 LCT 维护子树信息+树的重心

题目传送门

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

题解

首先每一个连通块的首都根据定义,显然就是直径。

然后考虑直径的几个性质:

  1. 定义:删去这个点以后剩下的连通块最大的最小的点为重心。
  2. 一棵树最多只能有两个相邻的直径;
  3. 一棵树的重心到一棵树中所有点的距离和最小。(这个也是题目的条件转化为重心的原因)
  4. 两棵树的并的重心在两棵树各自的重心的连线上。
  5. 一棵树添加或者删除一个节点,树的重心最多只移动一条边的位置。

有了这些性质,我们可以发现,两个连通块合并的时候,新的重心离较大的连通块的重心的距离不超过较小的连通块的大小。同时,新的重心在原来的两个中心之间。

那么我们就有了重心的移动方向和移动距离限制。

所以考虑启发式合并,均摊每次暴力移动 \(O(\log n)\) 次。每次移动求出权值需要子树大小来求。动态子树大小可以用 LCT 维护子树信息实现。


因此总的时间复杂度为 \(O(m\log^2 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 = 100000 + 7;

#define lc c[0]
#define rc c[1]

int n, m;
int sum;

struct Node { int c[2], s, sum, siz, fa, rev; } t[N];
int st[N];
inline bool idtfy(int o) { return t[t[o].fa].rc == o; }
inline bool isroot(int o) { return t[t[o].fa].lc != o && t[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) {
	t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
	t[o].siz = t[t[o].lc].siz + t[t[o].rc].siz + t[o].sum + 1;
}
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 = 0;
}
inline void rotate(int o) {
	int fa = t[o].fa, pa = 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);
}
inline void splay(int o) {
	int x = o, tp = 0;
	st[++tp] = x;
	while (!isroot(x)) st[++tp] = x = t[x].fa;
	while (tp) pushdown(st[tp--]);
	while (!isroot(o)) {
		int fa = t[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)  {
	for (int x = 0; o; o = t[x = o].fa) {
		splay(o);
		t[o].sum += t[t[o].rc].siz;
		t[o].sum -= t[x].siz;
		t[o].rc = x;
		pushup(o);
	}
}
inline void mkrt(int x) {
	access(x), splay(x);
	t[x].rev ^= 1, std::swap(t[x].lc, t[x].rc);
}
inline int getrt(int x) {
	access(x), splay(x);
	while (pushdown(x), t[x].lc) x = t[x].lc;
	splay(x);
	return x;
}
inline void link(int  x, int y) {
	mkrt(x);
	if (getrt(y) != x) {
		access(y), splay(y);
		t[x].fa = y, t[y].sum += t[x].siz, pushup(y);
	}
}

inline int dfs(int x, int sz, int &rt) {
	if (!x) return 0;
	pushdown(x);
	if (dfs(t[x].lc, sz, rt)) return 1;
	splay(x), pushdown(x);
	if ((t[x].sum + t[t[x].rc].siz + 1) * 2 > sz || ((t[x].sum + t[t[x].rc].siz + 1) * 2 == sz && x < rt)) rt = x;
	else return 1;
	if (dfs(t[x].rc, sz, rt)) return 1;
	return 0;
}

inline void work() {
	while (m--) {
		char opt[7];
		int x, y;
		scanf("%s", opt);
		if (*opt == 'X') printf("%d\n", sum);
		else if (*opt == 'Q') read(x), printf("%d\n", getrt(x));
		else {
			read(x), read(y);
			int px = getrt(x), py = getrt(y), rt;
			sum ^= px, sum ^= py;
			if (t[px].siz > t[py].siz || (t[px].siz == t[py].siz && px > py)) std::swap(x, y), std::swap(px, py);
			link(x, y), x = px, y = py;
			access(x), splay(y);
			dfs(y, t[y].siz, rt);
			mkrt(rt), sum ^= rt;
		}
	}
}

inline void init() {
	read(n), read(m);
	for (int i = 1; i <= n; ++i) sum ^= i, t[i].s = t[i].siz = 1;
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
posted @ 2019-10-21 17:34  hankeke303  阅读(202)  评论(0编辑  收藏  举报