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 ...
随机推荐
- 11.2 Flask 配置文件,路由系统
配置文件系统 构建 Flask 应用时指定 app = Flask( __name__, template_folder = '', # 指定存储模板文件夹名称 static_url_path = ' ...
- Magento 架构基础知识概述
Megento 架构基础知识概述 Magento整合了面向对象的基于PHP的应用程序的核心架构原则.这些一般原则的综合讨论既有在线的,也有印刷形式.以下讨论主要关注这些主题如何直接应用于Magento ...
- 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)
[HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...
- Python--Linux上安装Python
Linux 上安装 Python 官网下载:https://www.python.org/downloads/ 本文安装包下载链接:https://pan.baidu.com/s/1uL2JyoY_g ...
- request对象的方法及其参数的传递
先设计一个简单的登录界面index.htm: <html><head><title>request的使用</title></head>< ...
- python操作文件练习,配置haproxy
在使用python操作文件的时候,特别是对于网络设备,通常操作配置文件,会简化配置量,配置文件加载到内存中,运行时使用的是内存中的配置,内存中配置修改后立即生效,如果不将配置内容保存到硬盘中,则下次重 ...
- elk每日清除30天索引脚本
日常elk产生日志太多,故写个脚本放在定时任务,定时清理脚本 查询索引: curl -XGET 'http://127.0.0.1:9200/_cat/indices/?v' 删除索引: cu ...
- pkuseg:一个多领域中文分词工具包
pkuseg简单易用,支持细分领域分词,有效提升了分词准确度. 目录 主要亮点 编译和安装 各类分词工具包的性能对比 使用方式 相关论文 作者 常见问题及解答 主要亮点 pkuseg具有如下几个特点: ...
- Linux测试硬盘读性能的常用工具-hdparm和dd俩搭档
Linux测试硬盘读性能的常用工具-hdparm和dd俩搭档 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.hparm # 它用来在基于 Linux的系统上获取或 ...
- Entity Framework入门教程(7)--- EF中的查询方法
这里主要介绍两种查询方法 Linq to entity(L2E)和Sql 1.L2E查询 L2E查询时可以使用linq query语法,或者lambda表达式,默认返回的类型是IQueryable,( ...