题目传送门

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;
}

bzoj3510 首都 LCT 维护子树信息+树的重心的更多相关文章

  1. 【BZOJ3510】首都 LCT维护子树信息+启发式合并

    [BZOJ3510]首都 Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打 ...

  2. 【bzoj3510】首都 LCT维护子树信息(+启发式合并)

    题目描述 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从这个星球消失, ...

  3. BZOJ 3510: 首都 LCT + multiset维护子树信息 + 树的重心

    Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000 using namespace std; ...

  4. 【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息

    题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够联通的树上路过它的简单路径的数量 ...

  5. 【uoj#207】共价大爷游长沙 随机化+LCT维护子树信息

    题目描述 给出一棵树和一个点对集合S,多次改变这棵树的形态.在集合中加入或删除点对,或询问集合内的每组点对之间的路径是否都经过某条给定边. 输入 输入的第一行包含一个整数 id,表示测试数据编号,如第 ...

  6. 共价大爷游长沙 lct 维护子树信息

    这个题目的关键就是判断 大爷所有可能会走的路 会不会经过询问的边. 某一条路径经过其中的一条边, 那么2个端点是在这条边的2测的. 现在我们要判断所有的路径是不是都经过 u -> v 我们以u为 ...

  7. $LCT$维护子树信息学习笔记

    \(LCT\)维护子树信息学习笔记 昨天\(FDF\)好题分享投了 \([ZJOI2018]\)历史 这题. 然后我顺势学学这个姿势. 结果调了一年...于是写个笔记记录一下. 基本原理 比较显然地, ...

  8. 【LCT维护子树信息】uoj207 共价大爷游长沙

    这道题思路方面就不多讲了,主要是通过这题学一下lct维护子树信息. lct某节点u的子树信息由其重链的一棵splay上信息和若干轻儿子子树信息合并而成. splay是有子树结构的,可以在rotate, ...

  9. 洛谷4219 BJOI2014大融合(LCT维护子树信息)

    QWQ 这个题目是LCT维护子树信息的经典应用 根据题目信息来看,对于一个这条边的两个端点各自的\(size\)乘起来,不过这个应该算呢? 我们可以考虑在LCT上多维护一个\(xv[i]\)表示\(i ...

随机推荐

  1. debian配置国内软件源

    本例在debian:buster-slim docker镜像中实验通过 1.启动docker实例 docker run -it --name debian debian:buster-slim bas ...

  2. -----------------解决天天模拟器不能连接adb命令

    cmd------输入adb connect 127.0.0.1:6555即可 查询日志:adb shell "logcat |grep OkHttp"

  3. 使用js如何获取treeview控件的当前选中的节点

    var selectedNodeID = theForm.elements["<%=treeView1.ClientID%>_SelectedNode"].value; ...

  4. java网络通信:netty

    Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 也就是说,Netty 是一个基于NIO的客户,服务器端编程框架,使用Netty 可以确保 ...

  5. php5.6编译安装apache

    1.下载源码包wget 网址/php-5.6.30.tar.gz2.解压源码包tar -zxvf php-5.6.30.tar.gz3.创建一个安装目录mkdir /usr/local/php4.进入 ...

  6. 阶段3 1.Mybatis_10.JNDI扩展知识_1 补充-JNDI概述和原理

    H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\讲义+笔记+资料\主流框架\31.会员版(2.0)-就业课(2.0)-Mybatis\mybatis\mybatis_d ...

  7. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_4 Mybatis的CRUD-查询一个和模糊查询

    模糊查询 测试的时候需要提供百分号的模糊查询.传入的参数提供百分号 所有包含王字的就都查询出来了.

  8. 测开之路一百二十九:jinja2模板语法

    flask用的是jinja2模板,有自己特定的语法 形参: 在html里面留占位参数: {{ 参数名 }},后端传值时,参数名=参数值 <!DOCTYPE html><html la ...

  9. Linux安装python3.6 和pip

    Linux下安装Python3.6和第三方库   如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如yum!!! ...

  10. VBA文件操作

    做这些东西主要是为了,实现,我们的最终目标. 查到 两个大表里面的变化数据. 所以 这次 ①实现了 文件操作的一部分内容. 包括,excel的打开.分四个步骤. 1.路径 2.打开工作博 3.操作 4 ...