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 ...
随机推荐
- 详解javascript中this的工作原理
在 JavaScript 中 this 常常指向方法调用的对象,但有些时候并不是这样的,本文将详细解读在不同的情况下 this 的指向. 一.指向 window: 在全局中使用 this,它将会指向全 ...
- ExtJS初探:在项目中使用ExtJS
注意:本文写作时间是 2013 年,所讲的 ExtJS 如今早已过时,请勿学习! -------------------------------- 今天ExtJS官网发布了ExtJS最新正式版4.2. ...
- B. Heaters Div3
链接 [http://codeforces.com/contest/1066/problem/B] 分析 具体看代码,贪就完事了 代码 #include<bits/stdc++.h> us ...
- Abstractive Summarization
Sequence-to-sequence Framework A Neural Attention Model for Abstractive Sentence Summarization Alexa ...
- Linux内核分析— —扒开系统调用的三层皮(上)
实验部分 根据系统调用表,选取一个系统调用.我选得是mkdir这个系统调用,其系统调用号为39,即0x27 由于mkdir函数的原型为int mkdir (const char *filename, ...
- Proxy基础---------获取collection接口的构造跟方法
1----查看proxy api 2------测试代码 package cn.proxy01; import java.lang.reflect.Constructor; import java.l ...
- B01-java学习-阶段2-面向对象
对象内存分析 构造方法 类的深入解释 预定义类型和自定义类型深入分析和解释 预定义类源码的查看 预定义类和自定义类的对比 跨过类中使用自定义类型作为属性类型的门槛 构造方法的定义和执行过程 编译器提供 ...
- Flask-论坛开发-4-知识点补充
对Flask感兴趣的,可以看下这个视频教程:http://study.163.com/course/courseLearn.htm?courseId=1004091002 1. WTForms 表单使 ...
- css3-弹性盒模型
first <style> .box{width:1024px;height:100px; border:5px solid black; padding:10px; display:-w ...
- CRM 数据查重
2.8 小工具 · 纷享销客产品手册https://www.fxiaoke.com/mob/guide/crmdoc/src/2-8%E5%B0%8F%E5%B7%A5%E5%85%B7.html C ...