LOJ#6038. 「雅礼集训 2017 Day5」远行 [LCT维护子树的直径]
树的直径一定是原联通块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维护子树的直径]的更多相关文章
- LOJ#6038. 「雅礼集训 2017 Day5」远行(LCT)
题面 传送门 题解 要不是因为数组版的\(LCT\)跑得实在太慢我至于去学指针版的么--而且指针版的完全看不懂啊-- 首先有两个结论 1.与一个点距离最大的点为任意一条直径的两个端点之一 2.两棵树之 ...
- 【刷题】LOJ 6038 「雅礼集训 2017 Day5」远行
题目描述 Miranda 生活的城市有 \(N\) 个小镇,一开始小镇间没有任何道路连接.随着经济发现,小镇之间陆续建起了一些双向的道路但是由于经济不太发达,在建设过程中,会保证对于任意两个小镇,最多 ...
- loj#6038 「雅礼集训 2017 Day5」远行
分析 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define ...
- [loj6038]「雅礼集训 2017 Day5」远行 lct+并查集
给你 n 个点,支持 m 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. n≤3×10^5 n≤3×10^5 ,m≤5×10^5 m≤5 ...
- [LOJ#6039].「雅礼集训 2017 Day5」珠宝[决策单调性]
题意 题目链接 分析 注意到本题的 \(C\) 很小,考虑定义一个和 \(C\) 有关的状态. 记 \(f(x,j)\) 表示考虑到了价格为 \(x\) 的物品,一共花费了 \(j\) 元的最大收益. ...
- loj#6040. 「雅礼集训 2017 Day5」矩阵(线性代数+递推)
题面 传送门 题解 我的线代学得跟屎一样看题解跟看天书一样所以不要指望这题我会写题解 这里 //minamoto #include<bits/stdc++.h> #define R reg ...
- @loj - 6039@ 「雅礼集训 2017 Day5」珠宝
目录 @description@ @solution@ @accpeted code@ @details@ @description@ Miranda 准备去市里最有名的珠宝展览会,展览会有可以购买珠 ...
- loj #6039 「雅礼集训 2017 Day5」珠宝 分组背包 决策单调性优化
LINK:珠宝 去年在某个oj上写过这道题 当时懵懂无知wa的不省人事 终于发现这个东西原来是有决策单调性的. 可以发现是一个01背包 但是过不了 冷静分析 01背包的复杂度有下界 如果过不了说明必然 ...
- loj6038「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目传送门 https://loj.ac/problem/6038 题解 根据树的直径的两个性质: 距离树上一个点最远的点一定是任意一条直径的一个端点. 两个联通块的并的直径是各自的联通块的两条直径的 ...
随机推荐
- Golang import具体使用
使用gopath的时候,一般引用是从src下一层开始,比如src/github.com/…,引用github.com…,我的工程src/xxx.com/go-qb/…,引用xxx.com/go-qb/ ...
- C++ STL——优先队列的结构体表示方法
优先队列是队列的一种,但是自身具有一定的排序功能,所以不具有队列“先进先出”的性质 刚刚接触优先队列,看过网上的用法后感觉还是太过于朦胧,所以打算自己写一个稍微细节一点的. 头文件 #include& ...
- OSPFv3与OSPFv2协议的比较
From: http://blog.sina.com.cn/s/blog_61bd83dc0100la2u.html OSPFv3与OSPFv2协议的比较 OSPF是一种链路状态路由协议.它具有标 ...
- django后台处理前端上传和显示图片
1:项目根目录存放图片的目录 2:settings.py 添加 MEDIA_ROOT = os.path.join(BASE_DIR, "media") 3:url.py 添 ...
- postfix 被当作垃圾邮件中转站
磁盘 io 总是满的状态 该服务器只有监控和邮件elk在上面. 发现邮件日志 疯狂的输出 tail -f /var/log/maillog 大致都是来自于 yahoo.com.tw的东西 清空了 /v ...
- 详解Net Core Web Api项目与在NginX下发布
前言 本文将介绍Net Core的一些基础知识和如何NginX下发布Net Core的WebApi项目. 测试环境 操作系统:windows 10 开发工具:visual studio 2019 框架 ...
- ORB-SLAM2 论文&代码学习 —— LocalMapping 线程
转载请注明出处,谢谢 原创作者:Mingrui 原创链接:https://www.cnblogs.com/MingruiYu/p/12360913.html 本文要点: ORB-SLAM2 Local ...
- vue element 表头添加斜线
<template> <div class="app-container"> <el-table :data="tableData3&quo ...
- 一起了解 .Net Foundation 项目 No.6
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. .NET Micro Fr ...
- 将jsp页面转化为图片或pdf(一)(qq:1324981084)
java高级架构师全套vip教学视频,需要的加我qq1324981084 在项目中遇见了将jsp页面转化为pdf的问题,试过itext,但是itext需要标准的html代码,我的页面中的一些属性是it ...