分析

其实其他的题解说的都很清楚了。

一个点出发感染到根结点所花费的时间是路径上虚边的条数+1。

RELEASE相当于\(access()\)。

RECENTER相当于\(makeroot()\)。(虽然换根和打通路径的先后顺序不同但仔细想想本质其实是一样的)

所以我们可以通过维护一棵LCT来快速知道哪些结点与其父亲结点的连边由实变虚或由虚变实。

剩下的就是[BZOJ3083]遥远的国度了。

时间复杂度\(O(nlog^2n)\)。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <vector>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
#define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL; inline int read(){
int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x;
} const int MAXN=100005;
int n,m,root;
std::vector<int> e[MAXN],ee[MAXN];
int fa[MAXN],dep[MAXN],siz[MAXN],id[MAXN],num[MAXN],tot;
int sta[MAXN],top;
int ql,qr;
LL sum[MAXN<<2],atag[MAXN<<2],kk;
char opt[10];
struct lct{
int fa,ch[2];
int tag;
}a[MAXN]; inline void add_edge(int bg,int ed){
e[bg].push_back(ed);
} void dfs(int x,int pre,int depth){
fa[x]=pre;
a[x].fa=pre;
dep[x]=depth;
id[x]=++tot;
num[tot]=x;
siz[x]=1;
rin(i,0,(int)e[x].size()-1){
int ver=e[x][i];
if(ver==pre) continue;
ee[x].push_back(ver);
dfs(ver,x,depth+1);
siz[x]+=siz[ver];
}
} inline void subupd(int x,LL kkk); inline double subquery(int x); #define lc a[x].ch[0]
#define rc a[x].ch[1]
inline bool isroot(int x){
return a[a[x].fa].ch[0]!=x&&a[a[x].fa].ch[1]!=x;
} inline void pushr(int x){
std::swap(lc,rc);
a[x].tag^=1;
} inline void pushdown(int x){
if(!a[x].tag) return;
if(lc) pushr(lc);
if(rc) pushr(rc);
a[x].tag=0;
} inline void rotate(int x){
int y=a[x].fa,z=a[y].fa;
int f=(a[y].ch[1]==x),g=a[x].ch[f^1];
if(!isroot(y)) a[z].ch[a[z].ch[1]==y]=x;
a[x].ch[f^1]=y;
a[y].ch[f]=g;
if(g) a[g].fa=y;
a[y].fa=x;
a[x].fa=z;
} inline void splay(int x){
int y=x,z=0;
top=1;
sta[1]=y;
while(!isroot(y)) sta[++top]=y=a[y].fa;
while(top) pushdown(sta[top--]);
while(!isroot(x)){
y=a[x].fa,z=a[y].fa;
if(!isroot(y)){
if((a[y].ch[0]==x)==(a[z].ch[0]==y)) rotate(y);
else rotate(x);
}
rotate(x);
}
} inline int findroot(int x){
while(pushdown(x),lc) x=lc;
return x;
} inline void access(int x){
for(int y=0;x;x=a[y=x].fa){
splay(x);
if(rc){
subupd(findroot(rc),1);
}
rc=y;
if(rc){
subupd(findroot(rc),-1);
}
}
} inline void makeroot(int x){
access(x);
splay(x);
pushr(x);
}
#undef lc
#undef rc #define mid ((l+r)>>1)
#define lc (o<<1)
#define rc ((o<<1)|1)
inline void segpushdown(int o,int l,int r){
if(!atag[o]) return;
sum[lc]+=atag[o]*(mid-l+1);
sum[rc]+=atag[o]*(r-mid);
atag[lc]+=atag[o];
atag[rc]+=atag[o];
atag[o]=0;
} void build(int o,int l,int r){
if(l==r){
sum[o]=dep[num[l]];
return;
}
build(lc,l,mid);
build(rc,mid+1,r);
sum[o]=sum[lc]+sum[rc];
} void upd(int o,int l,int r){
if(ql>qr) return;
if(ql<=l&&r<=qr){
sum[o]+=kk*(r-l+1);
atag[o]+=kk;
return;
}
segpushdown(o,l,r);
if(mid>=ql) upd(lc,l,mid);
if(mid<qr) upd(rc,mid+1,r);
sum[o]=sum[lc]+sum[rc];
} LL query(int o,int l,int r){
if(ql>qr) return 0;
if(ql<=l&&r<=qr) return sum[o];
segpushdown(o,l,r);
LL ret=0;
if(mid>=ql) ret+=query(lc,l,mid);
if(mid<qr) ret+=query(rc,mid+1,r);
return ret;
}
#undef mid
#undef lc
#undef rc inline void subupd(int x,LL kkk){
kk=kkk;
if(id[root]<id[x]||id[root]>id[x]+siz[x]-1){
ql=id[x],qr=id[x]+siz[x]-1;
upd(1,1,n);
return;
}
else if(root==x){
ql=1,qr=n;
upd(1,1,n);
return;
}
else{
int l=0,r=(int)ee[x].size()-1,ver=0;
while(l<=r){
int mid=((l+r)>>1),verr=ee[x][mid];
if(id[verr]<=id[root]) ver=verr,l=mid+1;
else r=mid-1;
}
ql=1,qr=id[ver]-1;
upd(1,1,n);
ql=id[ver]+siz[ver],qr=n;
upd(1,1,n);
}
} inline double subquery(int x){
if(id[root]<id[x]||id[root]>id[x]+siz[x]-1){
ql=id[x],qr=id[x]+siz[x]-1;
return (double)query(1,1,n)/siz[x];
}
else if(root==x){
ql=1,qr=n;
return (double)query(1,1,n)/n;
}
else{
int l=0,r=(int)ee[x].size()-1,ver=0;
LL ret=0;
while(l<=r){
int mid=((l+r)>>1),verr=ee[x][mid];
if(id[verr]<=id[root]) ver=verr,l=mid+1;
else r=mid-1;
}
ql=1,qr=id[ver]-1;
ret+=query(1,1,n);
ql=id[ver]+siz[ver],qr=n;
ret+=query(1,1,n);
return (double)ret/(n-siz[ver]);
}
} int main(){
n=read(),m=read();
rin(i,2,n){
int u=read(),v=read();
add_edge(u,v);
add_edge(v,u);
}
root=1;
dfs(1,0,1);
build(1,1,n);
while(m--){
scanf("%s",opt+1);
int x=read();
if(opt[3]=='L'){
access(x);
}
else if(opt[3]=='C'){
makeroot(x);
root=x;
}
else{
printf("%.10lf\n",subquery(x));
}
}
return 0;
}

[BZOJ3779]重组病毒:Link-Cut Tree+线段树的更多相关文章

  1. [BZOJ3779]重组病毒(LCT+DFS序线段树)

    同[BZOJ4817]树点涂色,只是多了换根操作,分类讨论下即可. #include<cstdio> #include<algorithm> #define lc ch[x][ ...

  2. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  3. LCT(link cut tree) 动态树

    模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...

  4. Link Cut Tree 动态树 小结

    动态树有些类似 树链剖分+并查集 的思想,是用splay维护的 lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链 动态树的操作核心是把你要把 修改/询问/... 等 ...

  5. 洛谷.3690.[模板]Link Cut Tree(动态树)

    题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...

  6. 洛谷P3690 Link Cut Tree (动态树)

    干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...

  7. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  8. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  9. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

随机推荐

  1. Go语言入门篇-Golang之文本编码处理

    Golang之文本编码处理

  2. 个人推荐的两款vue导出EXCEL插件

    个人认为前端VUE项目中导出EXCEL比较好的两种方法,均不是我个人原创,我只是收录简单说明,原创地址在下面. 下面推荐两种方法,个人推荐第一种,第二种不做详细讲解,因为作者已经写过博客了,你们可以点 ...

  3. webpack e6转化成es5 配置方法

    方法一: https://www.babeljs.cn/setup#installation 按照babel官方的配置配 方法二: https://www.jianshu.com/p/ce28cedd ...

  4. Linux——临界段,信号量,互斥锁,自旋锁,原子操作

    一. linux为什么需要临界段,信号量,互斥锁,自旋锁,原子操作? 1.1. linux内核后期版本是支持多核CPU以及抢占式调度.这里就存在一个并发,竞争状态(简称竟态). 1.2. 竞态条件 发 ...

  5. 【LGR-065】洛谷11月月赛 III Div.2

    临近$CSP$...... 下午打了一发月赛,感觉很爽. 非常菜的我只做了前两题......然而听说前两题人均过...... 写法不优秀被卡到$#1067$...... T1:基础字符串练习题: 前缀 ...

  6. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  7. 【源码解读】cycleGAN(三):数据读取

    源码地址:https://github.com/aitorzip/PyTorch-CycleGAN 数据的读取是比较简单的,cycleGAN对数据没有pair的需求,不同域的两个数据集分别存放于A,B ...

  8. 分布式之redis(转发)

    为什么写这篇文章? 博主的<分布式之消息队列复习精讲>得到了大家的好评,内心诚惶诚恐,想着再出一篇关于复习精讲的文章.但是还是要说明一下,复习精讲的文章偏面试准备,真正在开发过程中,还是脚 ...

  9. React中配置Sass引入.scss文件无效

    React中配置Sass引入.scss文件无效 在react中使用sass时,引入.scss文件失效尝试很多方法没法解决,最终找到解决方法,希望能帮助正在坑里挣扎的筒子~ 在node_modules文 ...

  10. Apple Pay接入详细教程

    Apple Pay接入详细教程   来源:Yasin的简书 链接:http://www.jianshu.com/p/738aee78ba52# Apple Pay运行环境:iPhone6以上设备,操作 ...