bzoj3510 首都 LCT 维护子树信息+树的重心
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=3510
题解
首先每一个连通块的首都根据定义,显然就是直径。
然后考虑直径的几个性质:
- 定义:删去这个点以后剩下的连通块最大的最小的点为重心。
- 一棵树最多只能有两个相邻的直径;
- 一棵树的重心到一棵树中所有点的距离和最小。(这个也是题目的条件转化为重心的原因)
- 两棵树的并的重心在两棵树各自的重心的连线上。
- 一棵树添加或者删除一个节点,树的重心最多只移动一条边的位置。
有了这些性质,我们可以发现,两个连通块合并的时候,新的重心离较大的连通块的重心的距离不超过较小的连通块的大小。同时,新的重心在原来的两个中心之间。
那么我们就有了重心的移动方向和移动距离限制。
所以考虑启发式合并,均摊每次暴力移动 \(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;
}
bzoj3510 首都 LCT 维护子树信息+树的重心的更多相关文章
- 【BZOJ3510】首都 LCT维护子树信息+启发式合并
[BZOJ3510]首都 Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打 ...
- 【bzoj3510】首都 LCT维护子树信息(+启发式合并)
题目描述 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从这个星球消失, ...
- BZOJ 3510: 首都 LCT + multiset维护子树信息 + 树的重心
Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000 using namespace std; ...
- 【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息
题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够联通的树上路过它的简单路径的数量 ...
- 【uoj#207】共价大爷游长沙 随机化+LCT维护子树信息
题目描述 给出一棵树和一个点对集合S,多次改变这棵树的形态.在集合中加入或删除点对,或询问集合内的每组点对之间的路径是否都经过某条给定边. 输入 输入的第一行包含一个整数 id,表示测试数据编号,如第 ...
- 共价大爷游长沙 lct 维护子树信息
这个题目的关键就是判断 大爷所有可能会走的路 会不会经过询问的边. 某一条路径经过其中的一条边, 那么2个端点是在这条边的2测的. 现在我们要判断所有的路径是不是都经过 u -> v 我们以u为 ...
- $LCT$维护子树信息学习笔记
\(LCT\)维护子树信息学习笔记 昨天\(FDF\)好题分享投了 \([ZJOI2018]\)历史 这题. 然后我顺势学学这个姿势. 结果调了一年...于是写个笔记记录一下. 基本原理 比较显然地, ...
- 【LCT维护子树信息】uoj207 共价大爷游长沙
这道题思路方面就不多讲了,主要是通过这题学一下lct维护子树信息. lct某节点u的子树信息由其重链的一棵splay上信息和若干轻儿子子树信息合并而成. splay是有子树结构的,可以在rotate, ...
- 洛谷4219 BJOI2014大融合(LCT维护子树信息)
QWQ 这个题目是LCT维护子树信息的经典应用 根据题目信息来看,对于一个这条边的两个端点各自的\(size\)乘起来,不过这个应该算呢? 我们可以考虑在LCT上多维护一个\(xv[i]\)表示\(i ...
随机推荐
- EDM邮件营销真的落伍了吗?
很多朋友都觉得EDM邮件营销已经日暮西山了.难道EDM邮件营销真的落伍过时了吗?小编本文为大家讲解一下. 一.有数据为证:目前电子邮件仍然比较活跃,九成以上的用户每天至少查看一封邮件,并且6成以上的人 ...
- java网络通信:同步阻塞式I/O模型(BIO)
缺点:一个线程只能处理一个客户端连接 服务端: public class TimeServer { public static void main(String[] args) throws IOEx ...
- jmeter之报告输出(html)
在使用jmeter进行测试时,我们需要生成相应的测试报告,jmeter3.0之后有自带的测试报告. 在测试报告的格式和输出内容不满足需求时,我们可以根据需要去修改其配置文件(jmeter.proper ...
- multiple datasource config
Hi Harshit S. project structure: multiple datasource config as follows: step 1: step 2:add a datasou ...
- eclipse和myeclipse怎么在项目中查找指定代码?https://www.jb51.net/softjc/554889.html
有的童鞋,想eclipse和myeclipse整个项目中查找指定代码,由于补经常使用,可能会补熟悉.如果要去掉项目中所有的某个代码的话,找不到是灰常麻烦的,下面就简单说下怎么查找,希望对需要的人有用. ...
- c# 排列组合代码类
/// <summary> /// 排列组件算法类 /// </summary> /// <typeparam name="T"></ty ...
- 请求转发、包含、重定向 getAttribute 和 setAttribute POST和GET编码
一.请求转发 请求包含 请求重定向 Demo5.java 注意:doPost()方法中别忘写doGet(request, response); public void doGet(HttpS ...
- CSS3—— 多列 用户界面 图片 按钮
多列 将文本内容设计成像报纸一样的多列布局 多列创建 间隙 列边框 边框颜色+宽度 指定列的宽度 指定元素跨越多少列 用户界面 由用户调整元素大小[谷歌浏览器等] 以确切的方式定义适应某个区域的具体内 ...
- [Mac Terminal] ___MAC终端清屏快捷键
清全屏: command + K 清上一行命令:command + L
- postfix无法启动问题
open /etc/postfix/main.cf comment out inet_interfaces: all add inet_protocol: ipv4