loj6038「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT

题目传送门

https://loj.ac/problem/6038

题解

根据树的直径的两个性质:

  1. 距离树上一个点最远的点一定是任意一条直径的一个端点。

  2. 两个联通块的并的直径是各自的联通块的两条直径的四个端点的六个连线段之一。

于是我们可以维护每一个联通块的直径就可以了,这个可以用并查集实现。


但是从六条路径中选择直径需要求出每一条路径的长度,怎么求呢?

因为有强制在线部分,所以不能直接把树建立出来。

那就用 LCT 吧。


时间复杂度 (O(q(log n + alpha(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;
}

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

const int N = 300000 + 7;

int n, Q, tp;

struct Node { int s, c[2], fa, rev; } t[N];
int S[N];
inline bool isroot(int o) { return t[t[o].fa].lc != o && t[t[o].fa].rc != o; }
inline int idtfy(int o) { return 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; }
inline void pushdown(int o) {
	if (!t[o].rev) return;
	if (t[o].lc) std::swap(t[t[o].lc].lc, t[t[o].lc].rc), t[t[o].lc].rev ^= 1;
	if (t[o].rc) std::swap(t[t[o].rc].lc, t[t[o].rc].rc), t[t[o].rc].rev ^= 1;
	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];
	// dbg("o = %d, fa = %d, pa = %d, d1 = %d, d2 = %d, b= %d, isroot(fa) = %d, t[o].c = {%d, %d}, t[fa].c = {%d, %d}
", o, fa, pa, d1, d2, b, isroot(fa), t[o].c[0], t[o].c[1], t[t[o].fa].c[0], t[fa].c[1]);
	// dbg("****** %d %d %d %d
", o, t[o].fa, t[t[o].fa].fa, t[t[t[o].fa].fa].fa);
	if (!isroot(fa)) t[pa].c[d2] = o;
	t[o].fa = pa;
	// dbg("****** %d %d %d %d
", o, t[o].fa, t[t[o].fa].fa, t[t[t[o].fa].fa].fa);
	connect(o, fa, d1 ^ 1), connect(fa, b, d1);
	// dbg("****** %d %d %d %d
", o, t[o].fa, t[t[o].fa].fa, t[t[t[o].fa].fa].fa);
	pushup(fa), pushup(o);
	// dbg("****** %d %d %d %d lc = %d, rc = %d ,,,,%d, (%d, %d)
", o, t[o].fa, t[t[o].fa].fa, t[o].lc, t[o].rc, t[t[t[o].fa].fa].fa, t[t[o].fa].c[idtfy(o) ^ 1] != o, t[t[o].fa].c[0], t[t[o].fa].c[1]);
	assert(t[o].fa != o);
	assert(t[t[o].fa].c[idtfy(o) ^ 1] != o);
	assert(!fa || t[fa].fa != fa);
	assert(!t[o].fa || t[t[o].fa].fa != o);
	assert(!t[o].fa || !t[t[o].fa].fa || t[t[t[o].fa].fa].fa != o);
}
inline void splay(int o) {
	int x = o, tp = 1;
	S[tp] = x;
	while (!isroot(x)) S[++tp] = x = t[x].fa;
	while (tp) pushdown(S[tp--]);
	while (!isroot(o)) {
		int fa = t[o].fa;
		// dbg("in_splay: o = %d, fa = %d, isroot(o) = %d, isroot(fa) = %d
", o, fa, (int)isroot(o), (int)isroot(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) {
	// dbg("access : o = %d
", o);
	for (int x = 0; o; o = t[x = o].fa)
		splay(o), t[o].rc = x, pushup(o);
}
inline void mkrt(int o) {
	access(o), splay(o);
	t[o].rev ^= 1, std::swap(t[o].lc, t[o].rc);
}
inline int findrt(int o) {
	access(o), splay(o);
	// dbg("findrt: o = %d, t[o].lc = %d, t[o].fa = %d
", o, t[o].lc, t[o].fa);
	while (pushdown(o), t[o].lc) o = t[o].lc;
	splay(o);
	return o;
}
inline void link(int x, int y) {
	// dbg("link: x = %d, y = %d
", x, y);
	mkrt(x);
	if (findrt(y) != x) t[x].fa = y;
	// dbg("link: x = %d, y = %d
", x, y);
}
inline void cut(int x, int y) {
	mkrt(x), access(y), splay(y);
	if (t[y].lc == x && ~t[x].rc) t[y].lc  = t[x].fa = 0;
	pushup(y);
}
inline int dist(int x, int y) {
	// dbg("dist : x = %d, y = %d
", x, y);
	mkrt(x);
	access(y);
	splay(y);
	return t[y].s;
}

struct zj {
	int x, y, d;
	inline zj() {}
	inline zj(const int &x, const int &y) : x(x), y(y), d(dist(x, y)) {}
	inline bool operator < (const zj &b) const { return d < b.d; }
} b[N];

inline zj operator + (const zj &a, const zj &b) {
	zj ans = std::max(a, b);
	smax(ans, zj(a.x, b.x));
	if (b.y != b.x) smax(ans, zj(a.x, b.y));
	if (a.y != a.x) smax(ans, zj(a.y, b.x));
	if (a.y != a.x && b.y != b.x) smax(ans, zj(a.y, b.y));
	return ans;
}

int fa[N], siz[N];
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) {
	x = find(x), y = find(y);
	if (x == y) return;
	if (siz[x] < siz[y]) std::swap(x, y);
	fa[y] = x, smax(siz[x], siz[y] + 1);
	b[x] = b[x] + b[y];
}

inline void work() {
	int la = 0;
	for (int i = 1; i <= n; ++i) fa[i] = i, siz[i] = 1, b[i] = zj(i, i);
	while (Q--) {
		// dbg("Q = %d
", Q);
		int opt, x, y;
		read(opt), read(x);
		if (opt == 1) read(y), tp && (x ^= la, y ^= la), link(x, y), merge(x, y);
		else tp && (x ^= la), printf("%d
", la = std::max(dist(x, b[find(x)].x), dist(x, b[find(x)].y)) - 1);
	}
}

inline void init() {
	read(tp), read(n), read(Q);
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
原文地址:https://www.cnblogs.com/hankeke/p/loj6038.html