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 分块思想的更多相关文章

  1. 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 ...

  2. Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)

    题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...

  3. Codeforces 804D Expected diameter of a tree(树形DP+期望)

    [题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...

  4. CodeForces 805F Expected diameter of a tree 期望

    题意: 给出一个森林,有若干询问\(u, v\): 从\(u, v\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望. 分析: 回顾一下和树的直径有关的东西: 求树的直径 从树的任意 ...

  5. CF804D Expected diameter of a tree 树的直径 根号分治

    LINK:Expected diameter of a tree 1e5 带根号log 竟然能跑过! 容易想到每次连接两个联通快 快速求出直径 其实是 \(max(D1,D2,f_x+f_y+1)\) ...

  6. Codeforces Round #411 (Div. 1) D. Expected diameter of a tree

    题目大意:给出一个森林,每次询问给出u,v,问从u所在连通块中随机选出一个点与v所在连通块中随机选出一个点相连,连出的树的直径期望(不是树输出-1).(n,q<=10^5) 解法:预处理出各连通 ...

  7. codeforces804D Expected diameter of a tree

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. 543. Diameter of Binary Tree

    https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...

  9. 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 ...

随机推荐

  1. 【坦克大战】Unity3D多人在线游戏(泰课的坦克大战--旋转的螺丝钉)

    [坦克大战]Unity3D多人在线游戏 http://www.taikr.com/my/course/937 1.NetworkManager的介绍: 说明:选择固定生成时会自动寻找有StartPos ...

  2. 为什么要写 tf.Graph().as_default()

    首先,去tensorflow官网API上查询 tf.Graph() 会看到如下图所示的内容: 总体含义是说: tf.Graph() 表示实例化了一个类,一个用于 tensorflow 计算和表示用的数 ...

  3. LeetCode刷题(Java)

    第一题 class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = ...

  4. PR视频剪辑

    PR视频剪辑 新手问题1: 将素材导入到Adobe Premiere Pro CC后为什么无法拖入到时间轴上 解决办法:没有建立有序列所致,CC不会一开始就让你新建序列,图中间处写的好清楚“无序列”. ...

  5. Linux下 tftp 服务器的安装与使用

    安装步骤: 1. 安装xinetd, tftp-hpa tftpd-hpa a.  sudo apt-get install xinetd b.  sudo apt-get install tftp- ...

  6. 浏览器UI多线程及JavaScript单线程运行机制的理解

    在上一篇博客中,我对jQuery的队列(queue)机制和动画(animate)机制做了一个深入的解析,在animate的实现机制其核心是依靠queue来完成的,其中在jQuery的链式调用部分,之前 ...

  7. 16、爬取知乎大v张佳玮的文章“标题”、“摘要”、“链接”,并存储到本地文件

    爬取知乎大v张佳玮的文章“标题”.“摘要”.“链接”,并存储到本地文件 # 爬取知乎大v张佳玮的文章“标题”.“摘要”.“链接”,并存储到本地文件 # URL https://www.zhihu.co ...

  8. CSS margin负值学习及实际应用

    前言 margin属性在实际中非常常用,也是平时踩坑较多的地方.margin折叠部分相信不少人都因为这样那样的原因中过招.margin负值也是很常用的功能,很多特殊的布局方法都依赖于它. 表现 虽然m ...

  9. Love Me,Love My Dog

    Love me, love my dog. 爱屋及乌. 出处<尚书大传•大战>:“爱人者,兼其屋上之乌.” 活学活用:love first love 初恋 unrequited [one- ...

  10. 学习WPF

    http://www.cnblogs.com/prism/archive/2010/07/21/1781855.html 如何在WPF中画三角,以及把按钮设置成颜色渐变的样式: