树的直径一定是原联通块4个里的组合

1.LCT,维护树的直径,这题就做完了

2.直接倍增,lca啥的求求距离,也可以吧…

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
// #define int long long
using pii = pair<int, int>;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp<class T> void cmax(T& x, const T& y) {
if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
if (x > y) x = y;
}
// sort , unique , reverse
Tp<class T> void sort(ve<T>& v) { sort(all(v)); }
Tp<class T> void unique(ve<T>& v) {
sort(all(v));
v.erase(unique(all(v)), v.end());
}
Tp<class T> void reverse(ve<T>& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
~FILEIN() {}
char qwq[SZ], *S = qwq, *T = qwq, ch;
char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
FILEIN& operator>>(char& c) {
while (isspace(c = GETC()))
;
return *this;
}
FILEIN& operator>>(string& s) {
while (isspace(ch = GETC()))
;
s = ch;
while (!isspace(ch = GETC())) s += ch;
return *this;
}
Tp<class T> void read(T& x) {
bool sign = 1;
while ((ch = GETC()) < 0x30)
if (ch == 0x2d) sign = 0;
x = (ch ^ 0x30);
while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
x = sign ? x : -x;
}
FILEIN& operator>>(int& x) { return read(x), *this; }
// FILEIN& operator>>(signed& x) { return read(x), *this; }
FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
const static int LIMIT = 0x114514;
char quq[SZ], ST[0x114];
signed sz, O;
~FILEOUT() { sz = O = 0; }
void flush() {
fwrite(quq, 1, O, stdout);
fflush(stdout);
O = 0;
}
FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
FILEOUT& operator<<(string str) {
if (O > LIMIT) flush();
for (char c : str) quq[O++] = c;
return *this;
}
Tp<class T> void write(T x) {
if (O > LIMIT) flush();
if (x < 0) {
quq[O++] = 0x2d;
x = -x;
}
do {
ST[++sz] = x % 0xa ^ 0x30;
x /= 0xa;
} while (x);
while (sz) quq[O++] = ST[sz--];
return;
}
FILEOUT& operator<<(int x) { return write(x), *this; }
// FILEOUT& operator<<(signed x) { return write(x), *this; }
FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out; const int maxn = 3e5 + 10;
int f[maxn];
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
int px[maxn], py[maxn], fa[maxn], sz[maxn], rev[maxn], ch[maxn][2];
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
bool isroot(int x) { return (x != ls(fa[x])) && (x != rs(fa[x])); }
void pushup(int x) { sz[x] = sz[ls(x)] + sz[rs(x)] + 1; }
void pushr(int x) {
rev[x] ^= 1;
swap(ls(x), rs(x));
}
void pushdown(int x) {
if (!rev[x]) return;
if (ls(x)) pushr(ls(x));
if (rs(x)) pushr(rs(x));
rev[x] = 0;
}
void pushall(int x) {
if (!isroot(x)) pushall(fa[x]);
pushdown(x);
}
void rotate(int x) {
int y = fa[x], z = fa[y], l = (rs(y) == x), r = l ^ 1;
if (!isroot(y)) ch[z][rs(z) == y] = x;
fa[x] = z, fa[y] = x;
fa[ch[x][r]] = y, ch[y][l] = ch[x][r], ch[x][r] = y;
pushup(y), pushup(x);
}
void splay(int x) {
pushall(x);
while (!isroot(x)) {
int y = fa[x], z = fa[y];
if (!isroot(y)) rotate(((rs(y) == x) ^ (rs(z) == y)) ? x : y);
rotate(x);
}
}
void access(int x) {
for (int tp = 0; x; tp = x, x = fa[tp]) splay(x), rs(x) = tp, pushup(x);
}
void makeroot(int x) { access(x), splay(x), pushr(x); }
int dis(int x, int y) {
makeroot(x), access(y), splay(y);
return sz[y];
}
int qwq[5];
void link(int x, int y) {
int fx = find(x), fy = find(y);
int mx = -1, d = 0, rx, ry, cnt = 0;
makeroot(x), fa[x] = y;
qwq[++cnt] = px[fx], qwq[++cnt] = py[fx];
qwq[++cnt] = px[fy], qwq[++cnt] = py[fy];
for (int i = 1; i <= 4; i++)
for (int j = i + 1; j <= 4; j++) {
d = dis(qwq[i], qwq[j]);
if (d > mx) mx = d, rx = qwq[i], ry = qwq[j];
}
f[fx] = fy, px[fy] = rx, py[fy] = ry;
}
int type, n, q, opt, ans = 0;
signed main() {
#ifdef _WIN64
freopen("testdata.in", "r", stdin);
#else
ios_base ::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
#endif
// code begin.
in >> type >> n >> q;
for (int i = 1; i <= n; i++) f[i] = px[i] = py[i] = i, sz[i] = 1;
while (q--) {
int opt;
in >> opt;
if (opt == 1) {
int x, y;
in >> x >> y, x ^= ans, y ^= ans, link(x, y);
} else {
int x, fx;
in >> x, x ^= ans, fx = find(x);
ans = max(dis(x, px[fx]), dis(x, py[fx]));
ans--;
out << ans << '\n';
}
if (!type) ans = 0;
}
return out.flush(), 0;
// code end.
}

LOJ#6038. 「雅礼集训 2017 Day5」远行 [LCT维护子树的直径]的更多相关文章

  1. LOJ#6038. 「雅礼集训 2017 Day5」远行(LCT)

    题面 传送门 题解 要不是因为数组版的\(LCT\)跑得实在太慢我至于去学指针版的么--而且指针版的完全看不懂啊-- 首先有两个结论 1.与一个点距离最大的点为任意一条直径的两个端点之一 2.两棵树之 ...

  2. 【刷题】LOJ 6038 「雅礼集训 2017 Day5」远行

    题目描述 Miranda 生活的城市有 \(N\) 个小镇,一开始小镇间没有任何道路连接.随着经济发现,小镇之间陆续建起了一些双向的道路但是由于经济不太发达,在建设过程中,会保证对于任意两个小镇,最多 ...

  3. loj#6038 「雅礼集训 2017 Day5」远行

    分析 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define ...

  4. [loj6038]「雅礼集训 2017 Day5」远行 lct+并查集

    给你 n 个点,支持 m 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. n≤3×10^5 n≤3×10^5 ,m≤5×10^5 m≤5 ...

  5. [LOJ#6039].「雅礼集训 2017 Day5」珠宝[决策单调性]

    题意 题目链接 分析 注意到本题的 \(C\) 很小,考虑定义一个和 \(C\) 有关的状态. 记 \(f(x,j)\) 表示考虑到了价格为 \(x\) 的物品,一共花费了 \(j\) 元的最大收益. ...

  6. loj#6040. 「雅礼集训 2017 Day5」矩阵(线性代数+递推)

    题面 传送门 题解 我的线代学得跟屎一样看题解跟看天书一样所以不要指望这题我会写题解 这里 //minamoto #include<bits/stdc++.h> #define R reg ...

  7. @loj - 6039@ 「雅礼集训 2017 Day5」珠宝

    目录 @description@ @solution@ @accpeted code@ @details@ @description@ Miranda 准备去市里最有名的珠宝展览会,展览会有可以购买珠 ...

  8. loj #6039 「雅礼集训 2017 Day5」珠宝 分组背包 决策单调性优化

    LINK:珠宝 去年在某个oj上写过这道题 当时懵懂无知wa的不省人事 终于发现这个东西原来是有决策单调性的. 可以发现是一个01背包 但是过不了 冷静分析 01背包的复杂度有下界 如果过不了说明必然 ...

  9. loj6038「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT

    题目传送门 https://loj.ac/problem/6038 题解 根据树的直径的两个性质: 距离树上一个点最远的点一定是任意一条直径的一个端点. 两个联通块的并的直径是各自的联通块的两条直径的 ...

随机推荐

  1. SpringBoot学习笔记 文件访问映射

    通过SpringBoot可以把磁盘内所有的文件都访问到 有一张图片存放在 E://images/acti/123.jpg import org.springframework.context.anno ...

  2. 基于原生的 html css js php ajax做的一个 web登录和注册系统

    完整代码下载: 百度网盘地址 https://pan.baidu.com/s/1D1gqHSyjgfoOtYCZm7ofJg 提取码 :nf0b 永久有效 注意: 1 如果要正常运行此示例, 本地需要 ...

  3. golang搭建一个简单的web服务器

    package main import ( "io/ioutil" "log" "net/http" ) func main() { htt ...

  4. 数据算法 --hadoop/spark数据处理技巧 --(5.移动平均 6. 数据挖掘之购物篮分析MBA)

    五.移动平均 多个连续周期的时间序列数据平均值(按相同时间间隔得到的观察值,如每小时一次或每天一次)称为移动平均.之所以称之为移动,是因为随着新的时间序列数据的到来,要不断重新计算这个平均值,由于会删 ...

  5. python常用模块集合

    python常用模块集合 Python自定义模块 python collections模块/系列 Python 常用模块-json/pickle序列化/反序列化 python 常用模块os系统接口 p ...

  6. js模拟post提交表单

    function post(URL, PARAMS) {            var temp = document.createElement("form");         ...

  7. JVM性能优化系列-(6) 晚期编译优化

    6. 晚期编译优化 晚期编译优化主要是在运行时做的一些优化手段. 6.1 JIT编译器 在部分的商用虚拟机中,java程序最初是通过解释器(Interpreter) 进行解释执行的,当虚拟机发现某个方 ...

  8. JavaScript-装饰器模式

    装饰器模式 为对象添加新功能 不改变其原有的结构和功能 传统 UML 类图 javascript 中的装饰器 装饰类 @testDec clss Demo { } function testDec(t ...

  9. redis 5.0.7 源码阅读——字典dict

    redis中字典相关的文件为:dict.h与dict.c 与其说是一个字典,道不如说是一个哈希表. 一.数据结构 dictEntry typedef struct dictEntry { void * ...

  10. wifite硬核破解WiFi密码

    如题 楼主在这里分享下如何使用工具破解附近的WiFi 今天使用的工具是 wifite 现在都有WiFi万能钥匙了 暴力破解还有市场吗? 首先他俩的破解思路就不一样 wifi万能钥匙是根据云端数据库内容 ...