题意:点分治每次随机选重心,求期望复杂度。

发现一次点分治的复杂度就是点分树上每个节点的子树大小之和。(并没有发现......)

这个

注意这个写法有问题,随便来个菊花图就是n2了。

每一层点分治的时候,时间复杂度决不能与上一层大小挂钩。

 /**************************************************************
Problem: 3451
Language: C++
Result: Accepted
Time:5548 ms
Memory:8548 kb
****************************************************************/ #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath> typedef long long LL;
typedef long double LD;
const LD pi = 3.1415926535897932384626;
const int N = , INF = 0x3f3f3f3f; inline void read(int &x) {
x = ;
char c = getchar();
while(c < '' || c > '') {
c = getchar();
}
while(c >= '' && c <= '') {
x = (x << ) + (x << ) + c - ;
c = getchar();
}
return;
} struct cp {
LD x, y;
cp(LD X = , LD Y = ) {
x = X;
y = Y;
}
inline cp operator +(const cp &w) const {
return cp(x + w.x, y + w.y);
}
inline cp operator -(const cp &w) const {
return cp(x - w.x, y - w.y);
}
inline cp operator *(const cp &w) const {
return cp(x * w.x - y * w.y, x * w.y + y * w.x);
}
}a[N * ], b[N * ]; struct Edge {
int nex, v;
}edge[N << ]; int tp; int r[N * ], n, e[N], small, root, d[N], bin[N], cnt[N], _n, siz[N];
bool del[N]; inline void add(int x, int y) {
tp++;
edge[tp].v = y;
edge[tp].nex = e[x];
e[x] = tp;
return;
} inline void FFT(cp *a, int n, int f) {
for(register int i = ; i < n; i++) {
if(i < r[i]) {
std::swap(a[i], a[r[i]]);
}
}
for(register int len = ; len < n; len <<= ) {
cp Wn(cos(pi / len), f * sin(pi / len));
for(register int i = ; i < n; i += (len << )) {
cp w(, );
for(register int j = ; j < len; j++) {
cp t = a[i + len + j] * w;
a[i + len + j] = a[i + j] - t;
a[i + j] = a[i + j] + t;
w = w * Wn;
}
}
}
if(f == -) {
for(register int i = ; i <= n; i++) {
a[i].x /= n;
}
}
return;
} inline void cal(int n, int f) {
/*printf("cal \n");
for(int i = 0; i <= n; i++) {
printf("%d ", bin[i]);
}
printf("\n");*/
int lm = , len = ;
while(len <= n * ) {
len <<= ;
lm++;
}
for(register int i = ; i <= len; i++) {
r[i] = (r[i >> ] >> ) | ((i & ) << (lm - ));
}
for(register int i = ; i <= n; i++) {
a[i] = cp(bin[i], );
}
for(register int i = n + ; i <= len; i++) {
a[i] = cp(, );
}
FFT(a, len, );
for(register int i = ; i <= len; i++) {
a[i] = a[i] * a[i];
}
FFT(a, len, -);
/*for(int i = 0; i <= n + n; i++) {
printf("%d ", (int)(a[i].x + 0.5));
}
printf("\n");*/
for(register int i = ; i <= len; i++) {
cnt[i] += f * (int)(a[i].x + 0.5);
}
return;
} void get_root(int x, int f) {
bin[d[x]]++;
siz[x] = ;
int large = -;
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(y == f || del[y]) {
continue;
}
get_root(y, x);
siz[x] += siz[y];
large = std::max(large, siz[y]);
}
if(small > std::max(large, _n - siz[x])) {
small = std::max(large, _n - siz[x]);
root = x;
}
return;
} void DFS(int x, int f) {
d[x] = d[f] + ;
bin[d[x]]++;
siz[x] = ;
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(y == f || del[y]) {
continue;
}
DFS(y, x);
siz[x] += siz[y];
}
return;
} void div(int x, int f, int last_n) {
if(f) {
memset(bin, , sizeof(int) * (last_n + ));
}
small = INF;
get_root(x, );
if(f) {
cal(last_n, -);
}
x = root;
memset(bin, , sizeof(int) * (_n + ));
DFS(x, );
cal(_n, );
del[x] = ;
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(del[y]) {
continue;
}
_n = siz[y];
div(y, , siz[x]);
}
return;
} int main() {
d[] = -;
read(n);
for(register int i = , x, y; i < n; i++) {
read(x); read(y);
add(x + , y + ); add(y + , x + );
} _n = n;
div(, , n);
LD ans = ;
/*for(int i = 0; i <= n; i++) {
printf("%d ", cnt[i]);
}
printf("\n");*/
for(register int i = ; i < n; i++) {
ans += (LD)cnt[i] / (i + );
}
printf("%.4Lf\n", ans);
return ;
}

AC代码

注意FFT里面j从0开始,到len。

bzoj3451 Normal的更多相关文章

  1. [BZOJ3451]normal 点分治,NTT

    [BZOJ3451]normal 点分治,NTT 好久没更博了,咕咕咕. BZOJ3451权限题,上darkbzoj交吧. 一句话题意,求随机点分治的期望复杂度. 考虑计算每个点对的贡献:如果一个点在 ...

  2. [BZOJ3451]Normal(点分治+FFT)

    [BZOJ3451]Normal(点分治+FFT) 题面 给你一棵 n个点的树,对这棵树进行随机点分治,每次随机一个点作为分治中心.定义消耗时间为每层分治的子树大小之和,求消耗时间的期望. 分析 根据 ...

  3. BZOJ3451 Normal 期望、点分治、NTT

    BZOJCH传送门 题目大意:给出一棵树,求对其进行随机点分治的复杂度期望 可以知道一个点的贡献就是其点分树上的深度,也就是这个点在点分树上的祖先数量+1. 根据期望的线性性,考虑一个点对\((x,y ...

  4. 【BZOJ3451】Normal

    [BZOJ3451]Normal Description 某天WJMZBMR学习了一个神奇的算法:树的点分治! 这个算法的核心是这样的: 消耗时间=0 Solve(树 a) 消耗时间 += a 的 大 ...

  5. 【BZOJ3451】Normal (点分治)

    [BZOJ3451]Normal (点分治) 题面 BZOJ 题解 显然考虑每个点的贡献.但是发现似乎怎么算都不好计算其在点分树上的深度. 那么考虑一下这个点在点分树中每一次被计算的情况,显然就是其在 ...

  6. 【BZOJ3451】Tyvj1953 Normal 点分治+FFT+期望

    [BZOJ3451]Tyvj1953 Normal Description 某天WJMZBMR学习了一个神奇的算法:树的点分治!这个算法的核心是这样的:消耗时间=0Solve(树 a) 消耗时间 += ...

  7. BZOJ3451: Tyvj1953 Normal

    题解: 好神的一道题.蒟蒻只能膜拜题解. 考虑a对b的贡献,如果a是a-b路径上第一个删除的点,那么给b贡献1. 所以转化之后就是求sigma(1/dist(i,j)),orz!!! 如果不是分母的话 ...

  8. BZOJ3451 Tyvj1953 Normal 点分治 多项式 FFT

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3451.html 题目传送门 - BZOJ3451 题意 给定一棵有 $n$ 个节点的树,在树上随机点分 ...

  9. [BZOJ3451][Tyvj1953]Normal(点分治+FFT)

    https://www.cnblogs.com/GXZlegend/p/8611948.html #include<cmath> #include<cstdio> #inclu ...

随机推荐

  1. [UWP 自定义控件]了解模板化控件(1):基础知识

    1.概述 UWP允许开发者通过两种方式创建自定义的控件:UserControl和TemplatedControl(模板化控件).这个主题主要讲述如何创建和理解模板化控件,目标是能理解模板化控件常见的知 ...

  2. beta阶段性能指标测试

    性能指标概况 安装耗时 启动耗时 CPU占用 内存占用 电池温度 网络流量 平均值 5.48s 1.04s 1.61% 18.68MB 32.44℃ 93.78B 峰值 131.74s 5.13s 5 ...

  3. Week 2 代码审查

    我的伙伴是6班的小伙子潘礼鹏,经过几天的相处我觉得真的是说话很有趣的人,性格非常好,我们很划得来. 以下为我对他的代码的审查结果: VS2012与VS2013的兼容性 在这里写一个工具集的问题,不同的 ...

  4. Linux内核分析 期末总结

    Linux内核分析 期末总结 一.知识概要 1. 计算机是如何工作的 存储程序计算机工作模型:冯诺依曼体系结构 X86汇编基础 会变一个简单的C程序分析其汇编指令执行过程 2. 操作系统是如何工作的 ...

  5. a simple example for spring AOP

    /** * Created by Administrator on 2015/11/25. * a interface */public interface ArithmeticCalculator{ ...

  6. 转角遇见——Software

    第一部分:结缘计算机 从五岁开始读书,懵懵懂懂,从小就听长辈们说一定要考一个好大学,高三老师们就更是说:“过了高考,人生就无忧了”.于是似乎,高考就好像是我自出生以来这么多年的唯一愿景.高考成绩下来后 ...

  7. Orchard Core学习一

    Orchard Core学习一 Orchard Core是ASP.NET Core上Orchard CMS的重新开发. Orchard Core由两个不同的目标组成: Orchard核心框架:用于在A ...

  8. Centos wget命令 not found解决方法

    如果已经有了yun源,则可通过yun源命令来安装wget. 如下所示: 2.yum安装yum -y install wget 即可安装:

  9. Flask-论坛开发-4-知识点补充

    对Flask感兴趣的,可以看下这个视频教程:http://study.163.com/course/courseLearn.htm?courseId=1004091002 1. WTForms 表单使 ...

  10. K Nearest Neighbor 算法

    文章出处:http://coolshell.cn/articles/8052.html K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KN ...