Codeforces 840D Expected diameter of a tree 分块思想
我们先两次dfs计算出每个点能到达最远点的距离。
暴力计算两棵树x, y连边直径的期望很好求, 我们假设SZ(x) < SZ(y)
我们枚举 x 的每个端点, 二分找到分界点, 复杂度为SZ(x) * log(SZ(y))
其实我们对于每次询问我们记忆化一下就可以啦。
这是因为对于SZ(x)小于 sqrt(n)的询问, 我们直接暴力求就好啦, 复杂度q * SZ(x) * log(SZ(y))
对于SZ(x) > sqrt(n) 这样的 x , 个数绝对不超过sqrt(n)所以如果两两之间的答案全部算出来的
最坏复杂度是sqrt(n) * sqrt(n) / 2 * sqrt(n) * log(n) == n * sqrt(n) * log(n) 。 所以直接记忆化就好啦。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} const int B = ; map<PII, LL> Map;
int treecnt, belong[N];
vector<int> tree[N];
vector<LL> sum[N]; int n, m, q, maxdis[N], dia[N], son[N];
vector<int> G[N];
bool root[N]; void dfs(int u, int fa, int idx) {
belong[u] = idx;
son[idx]++;
for(auto& v : G[u]) {
if(v == fa) continue;
dfs(v, u, idx);
chkmax(maxdis[u], maxdis[v] + );
}
} void dfs2(int u, int fa, int maxup) {
chkmax(maxdis[u], maxup + );
int mx0 = maxup, mx1 = -inf;
for(auto& v : G[u]) {
if(v == fa) continue;
if(maxdis[v] > mx0) mx1 = mx0, mx0 = maxdis[v];
else if(maxdis[v] > mx1) mx1 = maxdis[v];
}
for(auto& v : G[u]) {
if(v == fa) continue;
if(maxdis[v] == mx0) dfs2(v, u, mx1 + );
else dfs2(v, u, mx0 + );
}
} LL calc(int u, int v) {
LL ans = ;
LL maxdia = max(dia[u], dia[v]);
for(auto& d : tree[u]) {
int p = upper_bound(ALL(tree[v]), maxdia - d - ) - tree[v].begin();
ans += p * maxdia;
if(p < SZ(tree[v])) {
ans += (SZ(tree[v]) - p) * (d + ) + sum[v].back();
if(p - >= ) ans -= sum[v][p - ];
}
}
return ans;
} int main() {
scanf("%d%d%d", &n, &m, &q);
for(int i = ; i <= m; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for(int i = ; i <= n; i++)
if(!belong[i]) dfs(i, , ++treecnt), root[i] = true;
for(int i = ; i <= n; i++) if(root[i]) dfs2(i, , -);
for(int i = ; i <= n; i++) {
tree[belong[i]].push_back(maxdis[i]);
chkmax(dia[belong[i]], maxdis[i]);
}
for(int i = ; i <= treecnt; i++) {
sort(ALL(tree[i]));
sum[i].resize(SZ(tree[i]));
sum[i][] = tree[i][];
for(int j = ; j < SZ(sum[i]); j++)
sum[i][j] = sum[i][j - ] + tree[i][j];
}
for(int i = ; i <= q; i++) {
int u, v; scanf("%d%d", &u, &v);
u = belong[u]; v = belong[v];
if(son[u] > son[v]) swap(u, v);
if(u == v) {
puts("-1");
} else {
if(Map.find(mk(u, v)) == Map.end()) Map[mk(u, v)] = calc(u, v);
printf("%.12f\n", 1.0 * Map[mk(u, v)] / son[u] / son[v]);
}
}
return ;
} /*
*/
Codeforces 840D Expected diameter of a tree 分块思想的更多相关文章
- Codeforces 804D Expected diameter of a tree
D. Expected diameter of a tree time limit per test 3 seconds memory limit per test 256 megabytes inp ...
- Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)
题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...
- Codeforces 804D Expected diameter of a tree(树形DP+期望)
[题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...
- CodeForces 805F Expected diameter of a tree 期望
题意: 给出一个森林,有若干询问\(u, v\): 从\(u, v\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望. 分析: 回顾一下和树的直径有关的东西: 求树的直径 从树的任意 ...
- CF804D Expected diameter of a tree 树的直径 根号分治
LINK:Expected diameter of a tree 1e5 带根号log 竟然能跑过! 容易想到每次连接两个联通快 快速求出直径 其实是 \(max(D1,D2,f_x+f_y+1)\) ...
- Codeforces Round #411 (Div. 1) D. Expected diameter of a tree
题目大意:给出一个森林,每次询问给出u,v,问从u所在连通块中随机选出一个点与v所在连通块中随机选出一个点相连,连出的树的直径期望(不是树输出-1).(n,q<=10^5) 解法:预处理出各连通 ...
- codeforces804D Expected diameter of a tree
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 543. Diameter of Binary Tree
https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...
- LeetCode 543. Diameter of Binary Tree (二叉树的直径)
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
随机推荐
- Bugku 分析 中国菜刀
解压之后得到了一个流量包,只有不到10KB,美滋滋 先抓重点,过滤出http,只有6条数据记录,3条post,3条response,3条post都是一样的 随便打开一条pos 是一个assert断言, ...
- 启动jar的方式
1.windows server 2008 start "srvRegistry" java -jar srvRegistry-1.0-SNAPSHOT.jar --spring. ...
- C/C++ const
一 含义 const修饰的变量在C和C++中的含义是什么?一样吗? 在C中用const修饰的变量仅仅表示这个符合表示的变量不能被赋值,是只读的,那么它与常量有啥区别呢?区别就是一个是常量,一个是变量. ...
- yii2 redirect重定向
redirect使用方法 $this->redirect(array('/site/contact','id'=>12)); //http://www.kuitao8.com/testwe ...
- vue实战记录(五)- vue实现购物车功能之商品总金额计算和单选全选删除功能
vue实战,一步步实现vue购物车功能的过程记录,课程与素材来自慕课网,自己搭建了express本地服务器来请求数据 作者:狐狸家的鱼 本文链接:vue实战-实现购物车功能(五) GitHub:sue ...
- 【UVA1660】Cable TV Network
题目大意:给定一个 N 个点的无向图,求至少删去多少个点可以使得无向图不连通. 题解:学习到了点边转化思想. 根据网络流的知识可知,一个网络的最小割与网络的最大流相等.不过最小割是图的边集,而本题则是 ...
- FreeNAS插件打造ownCloud私有云网盘
ownCloud 是一个自由开源的个人云存储解决方案,可以自由获取无需付费,但用户需要自行架设服务器,好在FreeNAS可以通过插件轻松的构建ownCloud服务器. ownCloud 分为服务器端和 ...
- 老男孩Python全栈学习 S9 日常作业 004
1.写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", "barry&q ...
- 散度、旋度与 Laplacian
$$\bex -\lap {\bf u}=\rot \rot {\bf u}-\n \Div {\bf u}. \eex$$
- 关于缓存和 Chrome 的“新版刷新”
在读本文前你要确保读过我的上篇文章<扼杀 304,Cache-Control: immutable>,因为本文是接着上文写的.上文说到,在现代 Web 上,“条件请求/304 响应”绝大多 ...