[loj6038]「雅礼集训 2017 Day5」远行 lct+并查集
给你 n 个点,支持 m 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林;询问一个点能到达的最远的点与该点的距离。强制在线。
n≤3×10^5 n≤3×10^5 ,m≤5×10^5 m≤5×10^5 。
我们知道与一个点距离最大的点为任意一个直径的两个端点之一。
两棵树之间连一条边,新树直径的两个端点一定为第一棵树直径的两个端点和第二棵树直径的两个端点这四者中之二。
于是我们可以用lct和并查集来维护树的直径的两个端点。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<algorithm>
#define fa(x) t[x].fa
#define s(x,y) t[x].son[y]
#define maxn 305050
using namespace std;
inline int read() {
int x=,f=;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-;
for(;isdigit(ch);ch=getchar()) x=x*+ch-'';
return x*f;
}
int type;
int n,q;
struct LCT {
int f[maxn],px[maxn],py[maxn];
int find(int x) {return x==f[x]?f[x]:f[x]=find(f[x]);}
struct Tree {int son[],fa,w,rev;}t[maxn];
bool isrt(int x) {return s(fa(x),)!=x&&s(fa(x),)!=x;}
void pushup(int x) {t[x].w=t[s(x,)].w+t[s(x,)].w+;}
void pushdown(int x) {
if(t[x].rev) {
swap(s(x,),s(x,));
t[s(x,)].rev^=;t[s(x,)].rev^=;
t[x].rev=;
}
}
void push(int x) {
if(!isrt(x)) push(fa(x));
pushdown(x);
}
void rotate(int x) {
int y=fa(x),z=fa(y);
int l=s(y,)==x,r=l^;
if(!isrt(y)) s(z,s(z,)==y)=x;
fa(x)=z;fa(y)=x;fa(s(x,r))=y;
s(y,l)=s(x,r);s(x,r)=y;
pushup(y);pushup(x);
}
void splay(int x) {
push(x);
while(!isrt(x)) {
int y=fa(x),z=fa(y);
if(!isrt(y)) {
if(s(z,)==y^s(y,)==x) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x) {for(int pre=;x;pre=x,x=fa(x)) {splay(x);s(x,)=pre;pushup(x);}}
void makert(int x) {access(x);splay(x);t[x].rev^=;}
int dis(int u,int v) {makert(u);access(v);splay(v);return t[v].w;}
void link(int u,int v) {
int fx=find(u),fy=find(v);
if(fx!=fy) {
makert(u);fa(u)=v;
int a=,b,c;
if(dis(px[fx],py[fx])>a) a=dis(px[fx],py[fx]),b=px[fx],c=py[fx];
if(dis(px[fy],py[fy])>a) a=dis(px[fy],py[fy]),b=px[fy],c=py[fy];
if(dis(px[fx],py[fy])>a) a=dis(px[fx],py[fy]),b=px[fx],c=py[fy];
if(dis(px[fx],px[fy])>a) a=dis(px[fx],px[fy]),b=px[fx],c=px[fy];
if(dis(py[fx],px[fy])>a) a=dis(py[fx],px[fy]),b=py[fx],c=px[fy];
if(dis(py[fx],py[fy])>a) a=dis(py[fx],py[fy]),b=py[fx],c=py[fy];
f[fx]=fy;px[fy]=b;py[fy]=c;
}
}
int query(int u) {
int x=find(u);
return max(dis(px[x],u),dis(py[x],u));
}
}lct;
int main() {
type=read();
n=read(),q=read();
for(int i=;i<=n;i++) lct.f[i]=lct.px[i]=lct.py[i]=i;
int lastans=;
while(q--) {
int t=read();
if(t==) {
int u=read()^(lastans*type),v=read()^(lastans*type);
lct.link(u,v);
}
else {
int u=read()^(lastans*type);
lastans=lct.query(u)-;
printf("%lld\n",lastans);
}
}
}
[loj6038]「雅礼集训 2017 Day5」远行 lct+并查集的更多相关文章
- loj6038「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目传送门 https://loj.ac/problem/6038 题解 根据树的直径的两个性质: 距离树上一个点最远的点一定是任意一条直径的一个端点. 两个联通块的并的直径是各自的联通块的两条直径的 ...
- LOJ#6038. 「雅礼集训 2017 Day5」远行 [LCT维护子树的直径]
树的直径一定是原联通块4个里的组合 1.LCT,维护树的直径,这题就做完了 2.直接倍增,lca啥的求求距离,也可以吧- // powered by c++11 // by Isaunoya #inc ...
- 【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目描述 给你 $n$ 个点,支持 $m$ 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. $n\le 3\times 10^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 ...
- 「雅礼集训 2017 Day5」珠宝
题目描述 Miranda 准备去市里最有名的珠宝展览会,展览会有可以购买珠宝,但可惜的是只能现金支付,Miranda 十分纠结究竟要带多少的现金,假如现金带多了,就会比较危险,假如带少了,看到想买的右 ...
- 「雅礼集训 2017 Day5」矩阵
填坑填坑.. 感谢wwt耐心讲解啊.. 如果要看这篇题解建议从上往下读不要跳哦.. 30pts 把$A$和$C$看成$n$个$n$维向量,那$A_i$是否加入到$C_j$中就可以用$B_{i,j}$表 ...
随机推荐
- bzoj2962 序列操作
2962: 序列操作 Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1145 Solved: 378[Submit][Status][Discuss ...
- bzoj 2530 [Poi2011]Party 构造
2530: [Poi2011]Party Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 364 Solved: ...
- LINUX下时间类API
(1)常用的时间相关的API和C库函数有9个:time/ctime/localtime/gmtime/mktime/asctime/strftime/gettimeofday/settimeofday ...
- webservice 采用SSL实现加密传输
本文转自:http://book.51cto.com/art/200906/129770.htm http://yeweiyun868.blog.163.com/blog/static/5637844 ...
- ACM-ICPC2018 沈阳赛区网络预赛-E-The cake is a lie
You promised your girlfriend a rounded cake with at least SS strawberries. But something goes wrong, ...
- 树链剖分处理+线段树解决问题 HDU 5029
http://acm.split.hdu.edu.cn/showproblem.php?pid=5029 题意:n个点的树,m次操作.每次操作输入L,R,V,表示在[L,R]这个区间加上V这个数字.比 ...
- NYOJ 305 表达式求值 (字符串处理)
题目链接 描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等.经过训练, ...
- koa源码阅读[1]-koa与koa-compose
接上次挖的坑,对koa2.x相关的源码进行分析 第一篇.不得不说,koa是一个很轻量.很优雅的http框架,尤其是在2.x以后移除了co的引入,使其代码变得更为清晰. express和koa同为一批人 ...
- CSS 中 nth-child 和 nth-of-type 的区别
假设有如下代码结构,想要查找 Piggy 那个 p <section> <h1>Words</h1> <p>Little</p> <p ...
- 2013-7-31hibernate二级缓存
难得闲 Fckeditor Fckconfig.js大部分配置都在这里面, 增加字体: 程序代码: FCKConfig.FontNames = 'Arial;Comic Sans MS ...