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 ...
随机推荐
- 关于计时器的js函数
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Drag(拖拽)和Move(移动)两个脚本
Drag using System.Collections; using System.Collections.Generic; using UnityEngine; public class Dra ...
- 清北澡堂 Day2 下午 一些比较重要的数论知识整理
1.欧拉定理 设x1,x2,.....,xk,k=φ(n)为1~n中k个与n互质的数 结论一:axi与axj不同余 结论二:gcd(axi,n)=1 结论三:x1,x2,...,xk和ax1,ax2, ...
- 机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)
假设有一段文本:"I have a cat, his name is Huzihu. Huzihu is really cute and friendly. We are good frie ...
- LoadRunner【第四篇】参数化
参数化的定义及使用场景 定义:将脚本中的特定值用变量替代,该变量值是变化的(注意:这个值是我们自己创建的,不是服务器返回的). 使用参数化: 1.业务考虑,不允许相同信息 2.真实模拟不同用户 3.真 ...
- BZOJ3784树上的路径
题目描述 给定一个N个结点的树,结点用正整数1..N编号.每条边有一个正整数权值.用d(a,b)表示从结点a到结点b路边上经过边的权值.其中要求a<b.将这n*(n-1)/2个距离从大到小排序, ...
- Nginx禁止IP直接访问网站
禁止别人直接通过IP访问网站,在nginx的server配置文件前面加上如下的配置,如果有通过IP直接访问的,直接拒绝连接(需要去掉别的server下的default_server). server ...
- CNN:Channel与Core的高H、宽W的权值理解
转自: 知乎问题[能否对卷积神经网络工作原理做一个直观的解释?https://www.zhihu.com/question/39022858]中YJango 的回答; 因总是忘记回答地址,方便以后查阅 ...
- python格式化输出的几种方式
第一种 字符串拼接 就不写了 下面的是 第二 第三 第四种 name = input("name:") age = int(input("age:")) p ...
- function Language
什么是函数式语言: 函数式语言(functional language)一类程序设计语言.是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定义函数和函数型.这种语言具有较强的组织数据 ...