[USACO18FEB]New Barns P
XV.[USACO18FEB]New Barns P
这种东西应该怎么维护呢?这是子树最大值呀。
一种方法是用平衡树(例如 std::multiset
)维护轻儿子长度集合。但是这种东西太麻烦,太恶心了。
考虑直径的性质。我们给出两条引理:
引理1:假如有一条直径,那么树中任意一个点到其它点的最远距离,一定为。
引理2:假如树有一条直径,树有一条直径为,则若我们把和添加一条边连起来,新的直径的两个端点,一定有。
有了这两个引理就OK了。我们需要用LCT动态查询树上两点距离,就是split
后计算size-1
。
然后,对于每颗树,维护直径。当加入新点后,计算中最长的路径作为直径即可。
代码:
#include<bits/stdc++.h>
using namespace std;
int n,dsu[100100],len[100100],cnt;
pair<int,int>p[100100];
#define lson t[x].ch[0]
#define rson t[x].ch[1]
struct LCT{
int fa,ch[2],sz;
bool rev;
}t[100100];
inline int identify(int x){
if(x==t[t[x].fa].ch[0])return 0;
if(x==t[t[x].fa].ch[1])return 1;
return -1;
}
inline void pushup(int x){
t[x].sz=1;
if(lson)t[x].sz+=t[lson].sz;
if(rson)t[x].sz+=t[rson].sz;
}
inline void REV(int x){
t[x].rev^=1,swap(lson,rson);
}
inline void pushdown(int x){
if(!t[x].rev)return;
if(lson)REV(lson);
if(rson)REV(rson);
t[x].rev=0;
}
inline void rotate(int x){
register int y=t[x].fa;
register int z=t[y].fa;
register int dirx=identify(x);
register int diry=identify(y);
register int b=t[x].ch[!dirx];
if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
if(b)t[b].fa=y;t[y].ch[dirx]=b;
t[y].fa=x,t[x].ch[!dirx]=y;
pushup(y),pushup(x);
}
inline void pushall(int x){
if(identify(x)!=-1)pushall(t[x].fa);
pushdown(x);
}
inline void splay(int x){
pushall(x);
while(identify(x)!=-1){
register int fa=t[x].fa;
if(identify(fa)==-1)rotate(x);
else if(identify(x)==identify(fa))rotate(fa),rotate(x);
else rotate(x),rotate(x);
}
}
inline void access(int x){
for(register int y=0;x;x=t[y=x].fa)splay(x),rson=y,pushup(x);
}
inline void makeroot(int x){
access(x),splay(x),REV(x);
}
inline int split(int x,int y){
makeroot(x),access(y),splay(y);
return t[y].sz-1;
}
inline void link(int x,int y){
makeroot(x),t[x].fa=y;
}
int main(){
scanf("%d",&n);
for(int i=1,x;i<=n;i++){
char s[3];
scanf("%s%d",s,&x);
if(s[0]=='B'){
++cnt;
t[cnt].sz=1;
if(x==-1){dsu[cnt]=cnt,p[cnt]=make_pair(cnt,cnt);continue;}
link(cnt,x),dsu[cnt]=dsu[x];
int nl=split(cnt,p[dsu[cnt]].first);
if(nl>len[dsu[cnt]]){p[dsu[cnt]]=make_pair(p[dsu[cnt]].first,cnt),len[dsu[cnt]]=nl;continue;}
nl=split(cnt,p[dsu[cnt]].second);
if(nl>len[dsu[cnt]]){p[dsu[cnt]]=make_pair(p[dsu[cnt]].second,cnt),len[dsu[cnt]]=nl;continue;}
}else{
int nl=split(x,p[dsu[x]].first);
nl=max(nl,split(x,p[dsu[x]].second));
printf("%d\n",nl);
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?