3451: Tyvj1953 Normal 点分治 FFT
国际惯例的题面:
代价理解为重心和每个点这个点对的代价。根据期望的线性性,我们枚举每个点,计算会产生的ij点对的代价即可。
那么,i到j的链上,i必须是第一个被选择的点。
对于i来说,就是1/dis(i,j)。
所以答案就是sigma(i,j) 1/(dis(i,j)+1)。
然而这样计算是n^2的,考虑优化。
如果我们能计算出边长为某个数值的边的数量的话,是不是就能计算答案呢?
统计路径的题,一眼点分治。
考虑怎样计算,我们能dfs出每个子树中距离分治重心为x的点有多少个,然后我们枚举两个点让他们取去组成路径即可。
这显然是个卷积,FFT优化。我们补集转化,先计算全部方案,再减去本身对本身(两个点来自相同子树)的方案。
为什么这样算复杂度正确?因为当当前分治层数一定时,所有子树的最深点的深度总和是O(n)的,并且那个log还会更小。这样分析的话发现复杂度是O(nlog^2n)。
正常的二元关系计算方式是前缀和和当前的卷积贡献,为什么这次不能这样呢?
给你一棵扫把形的树,一半的点形成一条链,显然你会选择扫把的重心(一边是一堆叶子,一边是链)当做重心。
然后你发现链的那边长度为n/2,如果你对每个叶子都和链做一次卷积的话,恭喜你卡成n^2logn,不如暴力......
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
const int maxn=;
const int inf=0x3f3f3f3f;
const double pi = acos(-1.0); int tim[maxn]; namespace FFT {
struct Complex {
double r,i;
friend Complex operator + (const Complex &a,const Complex &b) { return (Complex){a.r+b.r,a.i+b.i}; }
friend Complex operator - (const Complex &a,const Complex &b) { return (Complex){a.r-b.r,a.i-b.i}; }
friend Complex operator * (const Complex &a,const Complex &b) { return (Complex){a.r*b.r-a.i*b.i,a.r*b.i+a.i*b.r}; }
}cp[maxn];
inline void FFT(Complex* dst,int n,int tpe) {
for(int i=,j=;i<n;i++) {
if( i < j ) std::swap(dst[i],dst[j]);
for(int t=n>>;(j^=t)<t;t>>=) ;
}
for(int len=;len<=n;len<<=) {
const int h = len >> ;
const Complex per = (Complex){cos(pi*tpe/h),sin(pi*tpe/h)};
for(int st=;st<n;st+=len) {
Complex w = (Complex){1.0,0.0};
for(int pos=;pos<h;pos++) {
const Complex u = dst[st+pos] , v = dst[st+pos+h] * w;
dst[st+pos] = u + v , dst[st+pos+h] = u - v , w = w * per;
}
}
}
if( !~tpe ) for(int i=;i<n;i++) dst[i].r /= n;
}
inline void mul(int* dst,int n) {
int len = ;
while( len <= ( n << ) ) len <<= ;
for(int i=;i<len;i++) cp[i] = (Complex){(double)dst[i],0.0};
FFT(cp,len,);
for(int i=;i<len;i++) cp[i] = cp[i] * cp[i];
FFT(cp,len,-);
for(int i=;i<len;i++) dst[i] = (int)(cp[i].r+0.5);
}
} namespace Tree {
int s[maxn],t[maxn<<],nxt[maxn<<];
int siz[maxn],mxs[maxn],ban[maxn];
int su[maxn],tp[maxn]; inline void addedge(int from,int to) {
static int cnt = ;
t[++cnt] = to , nxt[cnt] = s[from] , s[from] = cnt;
}
inline void findroot(int pos,int fa,const int &fs,int &rt) {
siz[pos] = , mxs[pos] = ;
for(int at=s[pos];at;at=nxt[at]) if( t[at] != fa && !ban[t[at]] ) findroot(t[at],pos,fs,rt) , siz[pos] += siz[t[at]] , mxs[pos] = std::max( mxs[pos] , siz[t[at]] );
if( ( mxs[pos] = std::max( mxs[pos] , fs - siz[pos]) ) <= mxs[rt] ) rt = pos;
}
inline void dfs(int pos,int fa,int dep,int &mxd) {
mxd = std::max( mxd , dep ) , ++tp[dep];
for(int at=s[pos];at;at=nxt[at]) if( t[at] != fa && !ban[t[at]] ) dfs(t[at],pos,dep+,mxd);
}
inline void solve(int pos,int fs) {
int root = , mxd = , ths ;
*mxs = inf, findroot(pos,-,fs,root) , ban[root] = ;
for(int at=s[root];at;at=nxt[at]) if( !ban[t[at]]) {
ths = , dfs(t[at],root,,ths) , mxd = std::max( mxd , ths );
for(int i=;i<=ths;i++) su[i] += tp[i];
FFT::mul(tp,ths);
for(int i=;i<=ths<<;i++) tim[i] -= tp[i];
memset(tp,,sizeof(int)*(ths<<|));
}
++*su , FFT::mul(su,mxd);
for(int i=;i<=mxd<<;i++) tim[i] += su[i];
memset(su,,sizeof(int)*(mxd<<|));
for(int at=s[root];at;at=nxt[at]) if( !ban[t[at]] ) solve(t[at],siz[t[at]]<siz[root]?siz[t[at]]:fs-siz[root]);
}
} int main() {
static int n;
static long double ans;
scanf("%d",&n);
for(int i=,a,b;i<n;i++) scanf("%d%d",&a,&b) , ++a , ++b , Tree::addedge(a,b) , Tree::addedge(b,a);
Tree::solve(,n) , ans = n;
for(int i=;i<=n<<;i++) ans += (long double) tim[i] / ( i + );
printf("%0.4Lf\n",ans);
return ;
}
ここでこのまま
即使在这里就这样
僕が消えてしまっても 誰も知らずに
我消失不见了 谁也不会知道吧
明日が来るのだろう
明天依然会来临吧
わずか 世界のひとかけらに過ぎない
我仅仅是 这个世界的微小碎屑
ひとりを夜が包む
夜晚怀抱孤独的身影
3451: Tyvj1953 Normal 点分治 FFT的更多相关文章
- BZOJ 3451: Tyvj1953 Normal 点分治+FFT
根据期望的线性性,我们算出每个点期望被计算次数,然后进行累加. 考虑点 $x$ 对点 $y$ 产生了贡献,那么说明 $(x,y)$ 之间的点中 $x$ 是第一个被删除的. 这个期望就是 $\frac{ ...
- 【BZOJ3451】Tyvj1953 Normal 点分治+FFT+期望
[BZOJ3451]Tyvj1953 Normal Description 某天WJMZBMR学习了一个神奇的算法:树的点分治!这个算法的核心是这样的:消耗时间=0Solve(树 a) 消耗时间 += ...
- 【BZOJ3451】Tyvj1953 Normal - 点分治+FFT
题目来源:NOI2019模拟测试赛(七) 非原题面,题意有略微区别 题意: 吐槽: 心态崩了. 好不容易场上想出一题正解,写了三个小时结果写了个假的点分治,卡成$O(n^2)$ 我退役吧. 题解: 原 ...
- [BZOJ3451][Tyvj1953]Normal(点分治+FFT)
https://www.cnblogs.com/GXZlegend/p/8611948.html #include<cmath> #include<cstdio> #inclu ...
- bzoj 3451: Tyvj1953 Normal [fft 点分治 期望]
3451: Tyvj1953 Normal 题意: N 个点的树,点分治时等概率地随机选点,代价为当前连通块的顶点数量,求代价的期望值 百年难遇的点分治一遍AC!!! 今天又去翻了一下<具体数学 ...
- [BZOJ3451]Normal(点分治+FFT)
[BZOJ3451]Normal(点分治+FFT) 题面 给你一棵 n个点的树,对这棵树进行随机点分治,每次随机一个点作为分治中心.定义消耗时间为每层分治的子树大小之和,求消耗时间的期望. 分析 根据 ...
- BZOJ3451 Tyvj1953 Normal 点分治 多项式 FFT
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3451.html 题目传送门 - BZOJ3451 题意 给定一棵有 $n$ 个节点的树,在树上随机点分 ...
- BZOJ3451: Tyvj1953 Normal
题解: 好神的一道题.蒟蒻只能膜拜题解. 考虑a对b的贡献,如果a是a-b路径上第一个删除的点,那么给b贡献1. 所以转化之后就是求sigma(1/dist(i,j)),orz!!! 如果不是分母的话 ...
- 【bzoj3451】Tyvj1953 Normal 期望+树的点分治+FFT
题目描述 给你一棵 $n$ 个点的树,对这棵树进行随机点分治,每次随机一个点作为分治中心.定义消耗时间为每层分治的子树大小之和,求消耗时间的期望. 输入 第一行一个整数n,表示树的大小接下来n-1行每 ...
随机推荐
- oracle数据库自增主键重复
select max(t.id) from T_PLAT_ENUM_VALUE tdrop sequence T_PLAT_ENUM_VALUE;create sequence T_PLAT_ENUM ...
- MVC 带扩展名的路由无法访问
在MVC中,路由是必不可少的,而且MVC对Url的重写非常方便,只需要在路由中配置相应的规则即可.假如我们需要给信息详情页配置路由,代码如下: routes.MapRoute( name: " ...
- 006_netstat中state详解
TCP三次握手的过程如下: 主动连接端发送一个SYN包给被动连接端: 被动连接端收到SYN包后,发送一个带ACK和SYN标志的包给主动连接端: 主动连接端发送一个带ACK标志的包给被动连接端,握手动作 ...
- freeradius 错误: error:140890C7:SSL routines:ssl3_get_client_certificate:peer did not return a certificate
在进行802.1x 测试时遇到如下问题: Waking up in 4.6 seconds.(156) Received Access-Request Id 82 from 192.168.1.126 ...
- makefile 中autoload
在openwrt的makefile中经常能看见这样的描述: define KernelPackage/mt7602e CATEGORY:=MTK Properties TITLE:=MTK MT7 ...
- mysql5.6.13通用二进制格式安装并使用amoeba实现对mysql5.6数据库读写分离
proxy 192.168.8.39 master 192.168.8.40 slave 192.168.8.20 一.安装mysql-5.6.13服务器 安装包: mysql-5.6.13-linu ...
- php时间戳与日期转换
日期转换为时间戳 PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是: strtotime():将任何英文文本的日期时间描述解析为时间戳. mktime():从日期取得时间戳. ...
- npm下载速度过慢的解决办法
第一种方式: 在cmd 输入指令:npm config set registry https://registry.npm.taobao.org 不建议使用cnpm! 设置完后,注意检查:输入指令:n ...
- html 页内跳转
第一种 <a href="#div1">to div1</a> //跳转链接<div id="div1">div1</ ...
- pytest十六:allure2 生成 html 报告
allure 是一个 report 框架,支持 java 的 Junit/testng 等框架,当然也可以支持 python 的 pytest 框架,也可以集成到 Jenkins 上展示高大上的报告界 ...