loj6038「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目传送门
题解
根据树的直径的两个性质:
- 距离树上一个点最远的点一定是任意一条直径的一个端点。 
- 两个联通块的并的直径是各自的联通块的两条直径的四个端点的六个连线段之一。 
于是我们可以维护每一个联通块的直径就可以了,这个可以用并查集实现。
但是从六条路径中选择直径需要求出每一条路径的长度,怎么求呢?
因为有强制在线部分,所以不能直接把树建立出来。
那就用 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}\n", 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\n", 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\n", 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\n", 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)\n", 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\n", 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\n", 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\n", 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\n", x, y);
	mkrt(x);
	if (findrt(y) != x) t[x].fa = y;
	// dbg("link: x = %d, y = %d\n", 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\n", 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\n", 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\n", 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;
}
loj6038「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT的更多相关文章
- 【loj6038】「雅礼集训 2017 Day5」远行  树的直径+并查集+LCT
		题目描述 给你 $n$ 个点,支持 $m$ 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. $n\le 3\times 10^5$ ,$ ... 
- [loj6038]「雅礼集训 2017 Day5」远行 lct+并查集
		给你 n 个点,支持 m 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. n≤3×10^5 n≤3×10^5 ,m≤5×10^5 m≤5 ... 
- LOJ#6038. 「雅礼集训 2017 Day5」远行(LCT)
		题面 传送门 题解 要不是因为数组版的\(LCT\)跑得实在太慢我至于去学指针版的么--而且指针版的完全看不懂啊-- 首先有两个结论 1.与一个点距离最大的点为任意一条直径的两个端点之一 2.两棵树之 ... 
- 【刷题】LOJ 6038 「雅礼集训 2017 Day5」远行
		题目描述 Miranda 生活的城市有 \(N\) 个小镇,一开始小镇间没有任何道路连接.随着经济发现,小镇之间陆续建起了一些双向的道路但是由于经济不太发达,在建设过程中,会保证对于任意两个小镇,最多 ... 
- 「雅礼集训 2017 Day5」远行
		题目链接 问题分析 要求树上最远距离,很显然就想到了树的直径.关于树的直径,有下面几个结论: 如果一棵树的直径两个端点为\(a,b\),那么树上一个点\(v\)开始的最长路径是\(v\rightarr ... 
- loj#6038 「雅礼集训 2017 Day5」远行
		分析 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define ... 
- LOJ#6038. 「雅礼集训 2017 Day5」远行 [LCT维护子树的直径]
		树的直径一定是原联通块4个里的组合 1.LCT,维护树的直径,这题就做完了 2.直接倍增,lca啥的求求距离,也可以吧- // powered by c++11 // by Isaunoya #inc ... 
- 「雅礼集训 2017 Day5」珠宝
		题目描述 Miranda 准备去市里最有名的珠宝展览会,展览会有可以购买珠宝,但可惜的是只能现金支付,Miranda 十分纠结究竟要带多少的现金,假如现金带多了,就会比较危险,假如带少了,看到想买的右 ... 
- 「雅礼集训 2017 Day5」矩阵
		填坑填坑.. 感谢wwt耐心讲解啊.. 如果要看这篇题解建议从上往下读不要跳哦.. 30pts 把$A$和$C$看成$n$个$n$维向量,那$A_i$是否加入到$C_j$中就可以用$B_{i,j}$表 ... 
随机推荐
- struct files_struct
			内核利用文件描述符(file descriptor)来访问文件.文件描述符是非负整数.它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表.当程序打开一个现有文件或者创建一个新文件时,内 ... 
- What does the dot after dollar sign mean in jQuery when declaring variables?
			https://stackoverflow.com/questions/22156664/what-does-the-dot-after-dollar-sign-mean-in-jquery-when ... 
- jQuery实现三级联动菜单(鼠标悬停联动)
			效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> < ... 
- .net 4.5 Test Async Task 异步测试
			using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ... 
- bash如何向上向下移动历史命令记录
			bash如何在 历史命令记录 中上下移动? 通过bash本身的设置,就可以做到! : 用shift+up , shift+down 来上下移动 : 单纯的up/down是调用历史命令 
- python-笔记(六)模块操作以及常用模块简介
			模块.包 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思是说把python代码写到里面,文件名就是模块的名称,例如:model.py model就是模块名称. 什么是包? ... 
- MongoDB--(NoSQL)入门介绍
			NoSQL中比较优秀的产品 windows 下载安装 shell 基本操作(javascript 引擎) BSON扩充的数据类型(JSON的扩展,浮点型,日期型的扩充) step1.创建数据库 use ... 
- 百度人脸识别python调用例子
			# 首先pip install baidu-aip # SDK文档链接http://ai.baidu.com/docs#/Face-Python-SDK/top import base64 from ... 
- create-react-app 创建react应用环境变量(env)配置
			参考:https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables What other . ... 
- GMS认证测试FQA
			---摘要 本文档用于收录GMS认证测试的异常问题,提供一般性指导.对于本文档中未提供解答的问题请咨询@开发经理或@领域技术专家 cts测试工具如何获取? A:见Google官网 https://so ... 
