题目传送门

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. Mybaits 运行原理流程时序图

    1 .初始化sqlsessionFactory 2openSession 3.getMapper返回接口的代理对象 包含了SqlSession对象 4.查询流程

  2. ssm+ajax异步请求返回list遍历

    jsp页面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  3. 6.824 Lab 2: Raft 2A

    6.824 Lab 2: Raft Part 2A Due: Feb 23 at 11:59pm Part 2B Due: Mar 2 at 11:59pm Part 2C Due: Mar 9 at ...

  4. is_enabled()检查元素是否可以编辑 如文本框

    演示代码from selenium import webdriverdriver = webdriver.Firefox()driver.get("https://www.baidu.com ...

  5. 【读书笔记】Git使用

    初始设置本地Git 首先来设置使用 Git 时的姓名和邮箱地址.名字请用英文输入. $ git config --global user.name "Firstname Lastname&q ...

  6. IntelliJ IDEA 快捷键终极大全

    自动代码 常用的有fori/sout/psvm+Tab即可生成循环.System.out.main方法等boilerplate样板代码 . 例如要输入for(User user : users)只需输 ...

  7. [19/05/29-星期三] JavaScript_ 函数的简介

    一.函数的简介 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  8. java8--- Predicate 意义 代码

    //为了去除 DiyInterface 这个函数式接口,可以用通用函数式接口 Predicate 替代如下: https://blog.csdn.net/u011848397/article/deta ...

  9. Mybatis-学习笔记(5)动态SQL

    1.Mybatis采用功能强大的基于ONGL的表达式来完成动态SQL. 2.ONGL常用的元素有: 1>if <if test="id != null "> an ...

  10. promise和async/await的用法

    promise和async都是做异步处理的, 使异步转为同步 1.promise 它和Promise诞生的目的都是为了解决“回调地狱”, promise使用方法: <button @click= ...