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}$表 ...
随机推荐
- 大数据笔记(一)——Hadoop的起源与背景知识
一.大数据的5个特征(IBM提出): Volume(大量) Velocity(高速) Variety(多样) Value(价值) Varacity(真实性) 二.OLTP与OLAP 1.OLTP:联机 ...
- C#异常日志
代码比较简单,仅提供一种思路 /// <summary> /// 将异常打印到LOG文件 /// </summary> /// <param name="ex& ...
- Android单行跑马灯效果实现
参考网址:https://www.jianshu.com/p/e6c1b825d322 起初,使用了如下XML布局: <TextView android:id="@+id/tv_per ...
- React-Native 之 GD (七)下拉刷新 及 上拉加载更多
1.下拉刷新 使用第三方插件 下载插件: $ npm install react-native-pull@latest --save 引入: import {PullList} from 'reac ...
- redis集群安装多端口多实例部署
目标(本文达成的结果,配对关系可能会变): 先在131上进行操作 1.下载redis http://download.redis.io/releases/redis-5.0.2.tar.gz 2.解压 ...
- Android Bitmap变迁与原理解析(4.x-8.x)
App开发不可避免的要和图片打交道,由于其占用内存非常大,管理不当很容易导致内存不足,最后OOM,图片的背后其实是Bitmap,它是Android中最能吃内存的对象之一,也是很多OOM的元凶,不过,在 ...
- Oracle删除表时候有外键 不能删除
SELECT A .constraint_name, A .table_name, b.constraint_nameFROM user_constraints A, u ...
- TensorFlow学习笔记11-开始用TensorFlow
TensorFlow运作方式 要用到的代码都在Github上.当然,如果你本地装了TensorFlow,也可以用Everything直接搜索以下文件: mnist.py fully_connected ...
- 关于fork
关于fork 之前和同学讨论了一个关于fork的问题,之前自己也是稍微看过一点,但是具体的也不是太了解,这样还是很不好的. 具体的问题来源于一个面试题,大概是问 fork||fork操作会生成几个新的 ...
- C#网络编程 多线程和高并发
在任何 TCP Server 的实现中,一定存在一个 Accept Socket Loop,用于接收 Client 端的 Connect 请求以建立 TCP Connection. 在任何 TCP S ...