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 题解 根据树的直径的两个性质: 距离树上一个点最远的点一定是任意一条直径的一个端点. 两个联通块的并的直径是各自的联通块的两条直径的 ...
随机推荐
- DRF框架的安装与使用
目录 DRF框架的安装与配置 基于restful接口规范的接口设计 DRF框架的安装与配置 """ 1)安装 >: pip install djangorestfr ...
- gcc和g++的区别:安装、版本、编译(转)
用以下命令: yum install gcc 安装的只有gcc,而不会安装g++.gcc是编译器合集,而gcc-g++或简称g++则是C++编译器.gcc成为了编译器的选择器.gcc通过识别被编译的源 ...
- create-react-app 打包后静态文件过大 webpack优化
在最近的项目里,页面和静态文件并不是很多的情况下,打包后发现产出的静态资源却很大. 1.关掉sourcemap 在config/webpack.config.js文件里,大概30几行的位置添加这样一句 ...
- react项目中引用amap(高德地图)坑
最近在写一个react项目,用到了需要定位的需求,于是乎自己决定用高德地图(AMap),但是react官方文档的案列很少,大多都是原生JS的方法. 在调用amap的 Geocoder Api 时,一直 ...
- 阿里云服务器ECS Ubuntu18.04 安装mysql
ubuntu系统好了,这下我应该安装MySQL数据库了.在安装过程中,遇到好多坑,下面是我的安装过程. 1.在阿里云控制台,用vnc登录到服务器. 用新的用户登录到Ubuntu用户系统. 打开终端: ...
- 开源堡垒机jumpserver
开源堡垒机jumpserver 开源堡垒机jumpserver的安装 开源堡垒机jumpserver的配置和使用
- Force removing ActivityRecord no saved state问题的原因分析
关键字,crash Force removing ActivityRecord,app died, no saved state的原因 相机拍摄的过程中断电,拍了一张黑图 I/ActivityMana ...
- 1138 - Trailing Zeroes (III) 二分
1138 - Trailing Zeroes (III) You task is to find minimal natural number N, so that N! contains exa ...
- 浅谈Linux与unix系统的来历
在今天的UNIX是商业化的,UNIX系统大多是与硬件配套的,也就是说,大多数UNIX系统如AIX.HP-UX等是无法安装在 x86 服务器和个人计算机上的,UNIX系统是一个分时系统,而UNIX是至关 ...
- Java的引用类型的内存分析
一. jdk的内存:jdk的bin目录常见命令 1. javac.exe:编译java源代码的,生成java字节码文件(*.class) 2. java.exe:启动一个jvm,来运行指定class字 ...