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}$表 ...
随机推荐
- CMD模块打包部署总结
目前线上系统利用Seajs做模板化,但是没有对js和css进行打包,在这次简历搜索优化项目里我尝试对gulp插件对Seajs模块打包. 安装gulp和相关插件 npm install -g gulp ...
- linux 下spyder安装
linux 下spyder安装: 安装qt4,python3 对应qt5 sudo apt-get install libxext6 libxext-dev libqt4-dev libqt4-gu ...
- 用Vue来实现购物车功能(二)
这个小demo具有添加商品进购物车 .增加购物车内商品的数量.减少购物车内商品的数量.计算一类商品的总价.以及计算所有商品的总价 首先看目录结构 因为我们的Tab.vue Car.vue 以及Car ...
- Java ——Scanner
本节重点思维导图 import java.util.Scanner;//导包 public class Demo { public static void main(String[] args) { ...
- 【MM系列】SAP MM模块-基础配置第一篇
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-基础配置第一篇 ...
- vue子组件修改父组件传递过来的值
这里不再赘述父子组件及子父组件传值,不懂的同学可以翻看我以前写过的关于两者传值的文章 父子组件传值:https://www.cnblogs.com/Sky-Ice/p/9267192.html 子父组 ...
- postman从body,headers,data中获取token后回写做全局变量
设置全局变量
- 如何判断一段程序是由C 编译程序还是由C++编译程序编译的
以下是在论坛中看到的两种解释: (1)如果是要你的代码在编译时发现编译器类型,就判断_cplusplus或_STDC_宏,通常许多编译器还有其他编译标志宏, #ifdef __cplusplus co ...
- 转 jvisualvm 工具使用 https://www.cnblogs.com/kongzhongqijing/articles/3625340.html
VisualVM 是Netbeans的profile子项目,已在JDK6.0 update 7 中自带(java启动时不需要特定参数,监控工具在bin/jvisualvm.exe). https:// ...
- union,union all, Intersect和Minus [转]
Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...