bzoj3451 Normal
题意:点分治每次随机选重心,求期望复杂度。
发现一次点分治的复杂度就是点分树上每个节点的子树大小之和。(并没有发现......)
看这个。
注意这个写法有问题,随便来个菊花图就是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的更多相关文章
- [BZOJ3451]normal 点分治,NTT
[BZOJ3451]normal 点分治,NTT 好久没更博了,咕咕咕. BZOJ3451权限题,上darkbzoj交吧. 一句话题意,求随机点分治的期望复杂度. 考虑计算每个点对的贡献:如果一个点在 ...
- [BZOJ3451]Normal(点分治+FFT)
[BZOJ3451]Normal(点分治+FFT) 题面 给你一棵 n个点的树,对这棵树进行随机点分治,每次随机一个点作为分治中心.定义消耗时间为每层分治的子树大小之和,求消耗时间的期望. 分析 根据 ...
- BZOJ3451 Normal 期望、点分治、NTT
BZOJCH传送门 题目大意:给出一棵树,求对其进行随机点分治的复杂度期望 可以知道一个点的贡献就是其点分树上的深度,也就是这个点在点分树上的祖先数量+1. 根据期望的线性性,考虑一个点对\((x,y ...
- 【BZOJ3451】Normal
[BZOJ3451]Normal Description 某天WJMZBMR学习了一个神奇的算法:树的点分治! 这个算法的核心是这样的: 消耗时间=0 Solve(树 a) 消耗时间 += a 的 大 ...
- 【BZOJ3451】Normal (点分治)
[BZOJ3451]Normal (点分治) 题面 BZOJ 题解 显然考虑每个点的贡献.但是发现似乎怎么算都不好计算其在点分树上的深度. 那么考虑一下这个点在点分树中每一次被计算的情况,显然就是其在 ...
- 【BZOJ3451】Tyvj1953 Normal 点分治+FFT+期望
[BZOJ3451]Tyvj1953 Normal Description 某天WJMZBMR学习了一个神奇的算法:树的点分治!这个算法的核心是这样的:消耗时间=0Solve(树 a) 消耗时间 += ...
- BZOJ3451: Tyvj1953 Normal
题解: 好神的一道题.蒟蒻只能膜拜题解. 考虑a对b的贡献,如果a是a-b路径上第一个删除的点,那么给b贡献1. 所以转化之后就是求sigma(1/dist(i,j)),orz!!! 如果不是分母的话 ...
- BZOJ3451 Tyvj1953 Normal 点分治 多项式 FFT
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3451.html 题目传送门 - BZOJ3451 题意 给定一棵有 $n$ 个节点的树,在树上随机点分 ...
- [BZOJ3451][Tyvj1953]Normal(点分治+FFT)
https://www.cnblogs.com/GXZlegend/p/8611948.html #include<cmath> #include<cstdio> #inclu ...
随机推荐
- Linux下对文件进行加密备份的操作记录
由于公司之前在阿里云上购买了一些机器,后续IDC建设好后,又将线上业务从阿里云上迁移到IDC机器上了,为了不浪费阿里云上的这几台机器资源,打算将这些机器做成IP SAN共享存储,然后作为IDC数据的一 ...
- jenkins中配置svn 出现absolute path is not allowed
代码: 兵马未动,粮草先行 作者: 传说中的汽水枪 如有错误,请留言指正,欢迎一起探讨. 转载请注明出处. 想用jenkins作自动化部署tomcat. svn代码已经checkout到本地目录了(/ ...
- 【补充】第一次个人项目出现的bug
新程序包下载(密码:4kp6) >>>>>直接上代码,问题出在随机分数的生成上,确实出现了一些非常鱼唇的错误,不过已经提交了就没办法了,在这里发出来仅供参考吧: 修改前: ...
- Maven入门指南④:仓库
1 . 仓库简介 没有 Maven 时,项目用到的 .jar 文件通常需要拷贝到 /lib 目录,项目多了,拷贝的文件副本就多了,占用磁盘空间,且难于管理.Maven 使用一个称之为仓库的目录,根据构 ...
- 速读《构建之法》(Build to win)有感
通过这两天时间,我粗读了<构建之法>这本书.老实说,对于这样四百多页的一本书,刚开始把这样的任务当作是一种负担,然而当我开始真正接触它时却被它幽默有趣的风格所深深吸引,它不同于以往学习的教 ...
- 2017[BUAA软工]第0次个人作业
第一部分:结缘计算机 1.你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? ●其实填写志愿之前并不知道要学什么专业,当初选择计算机是因为计算机就业前景好.方向多.计算机应用的领域无处不在, ...
- Docker(十九)-Docker监控容器资源的占用情况
启动一个容器并限制资源 启动一个centos容器,限制其内存为1G ,可用cpu数为2 [root@localhost ~]# docker run --name os1 -it -m 1g --cp ...
- From 简书 转帖一下如何安装k8s1.10 改天做下实验. https://www.jianshu.com/p/9c7e1c957752
centos7.3 kubernetes/k8s 1.10 离线安装 老菜_misa 关注 2018.04.25 23:57 字数 1243 阅读 266评论 1喜欢 3 本文介绍在centos7.3 ...
- mysql数据库优化大全
转载:https://blog.csdn.net/weixin_38112233/article/details/79054661 数据库优化 sql语句优化 索引优化 加缓存 读写分离 分区 分布式 ...
- QC
IQC:Incoming Quality Control 意思是来料的质量控制 来料 IPQC:InPut Process Quality Control 过程质量控制 来料 FQC:Final ...