CodeForces 805F Expected diameter of a tree 期望
题意:
给出一个森林,有若干询问\(u, v\):
从\(u, v\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望。
分析:
回顾一下和树的直径有关的东西:
求树的直径
从树的任意一点出发搜到最远的一点\(x\),再从\(x\)出发搜到距\(x\)最远的一点\(y\),那么\(d(x,y)\)就是树的直径。时间复杂度为\(O(n)\)。
求构成新树的直径
假设原来两棵树的直径分别问\(d_1,d_2\)
令\(f_i\)为点\(i\)所在子树中距它最远的点的距离
新树的直径要么在原来两棵树中\(max(d_1,d_2)\),要么经过添加的边\(u \to v\)为\(f_u + f_v + 1\)
新的直径为两种情况取最大值
计算\(f_i\)
对于每个点\(i\)计算出距它最远的距离,只要分别从直径的两端各\(DFS\)一次即可,保存最大值。
也就是说,距离\(i\)最远的点是直径两个端点其中之一。
处理询问
只用考虑询问两点在不同子树中的情况:
枚举一棵子树中的\(f_u\),对另一棵树中的\(f\)排序。
二分或者尺取出\(f_u + f_v + 1 \leq max(d_1, d_2)\)的个数,分别统计出答案。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define PER(i, a, b) for(int i = b - 1; i >= a; i--)
#define SZ(a) ((int)a.size())
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(a) a.begin(), a.end()
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 100000 + 10;
int n, m, q;
vector<int> G[maxn], dis[maxn];
vector<LL> pre[maxn];
int f[maxn], d[maxn], cnt;
map<PII, LL> ans;
int pa[maxn], sz[maxn];
int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); }
void Union(int x, int y) {
int px = findset(x), py = findset(y);
if(px != py) {
pa[px] = py;
sz[py] += sz[px];
}
}
int id, depth, root, flag;
void dfs(int u, int fa = -1, int h = 0) {
if(h > depth) { depth = h; id = u; }
if(f[u] < h) f[u] = h;
if(flag) dis[root].PB(f[u]);
for(int v : G[u]) if(v != fa) dfs(v, u, h + 1);
}
int main() {
scanf("%d%d%d", &n, &m, &q);
REP(i, 0, n) pa[i] = i, sz[i] = 1;
while(m--) {
int u, v; scanf("%d%d", &u, &v);
u--; v--;
Union(u, v);
G[u].push_back(v);
G[v].push_back(u);
}
REP(i, 0, n) if(i == pa[i]) {
flag = false; root = i;
depth = 0; id = i; dfs(i);
depth = 0; dfs(id); d[i] = depth;
flag = true; dfs(id);
sort(ALL(dis[i]));
pre[i].resize(sz[i] + 1);
pre[i][0] = 0;
REP(j, 0, SZ(dis[i])) pre[i][j + 1] = pre[i][j] + dis[i][j];
}
while(q--) {
int u, v; scanf("%d%d", &u, &v);
u--; v--;
u = findset(u), v = findset(v);
if(u == v) { printf("-1\n"); continue; }
if(sz[u] > sz[v] || (sz[u] == sz[v] && u > v)) swap(u, v);
if(ans.count(MP(u, v))) { printf("%.10f\n", (double)ans[MP(u, v)] / sz[u] / sz[v]); continue; }
int maxd = max(d[u], d[v]);
int p = sz[v] - 1;
LL t = 0;
for(int x : dis[u]) {
while(p >= 0 && x + dis[v][p] + 1 > maxd) p--;
t += (LL)maxd * (p+1) + (LL)(x+1)*(sz[v]-1-p) + pre[v].back()-pre[v][p+1];
}
ans[MP(u, v)] = t;
printf("%.10f\n", (double)t / sz[u] / sz[v]);
}
return 0;
}
CodeForces 805F Expected diameter of a tree 期望的更多相关文章
- Codeforces 840D Expected diameter of a tree 分块思想
Expected diameter of a tree 我们先两次dfs计算出每个点能到达最远点的距离. 暴力计算两棵树x, y连边直径的期望很好求, 我们假设SZ(x) < SZ(y) 我们枚 ...
- 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所在连通块中随 ...
- 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 ...
随机推荐
- 拼接sql语句时拼接空字符串报sql错误
先上代码(php): $id_card=""; $sql = "select * from people where id_card=".$id_card; 看 ...
- COGS 2091. Asm.Def的打击序列
★★★ 输入文件:asm_lis.in 输出文件:asm_lis.out 简单对比时间限制:4 s 内存限制:256 MB [题目描述] 白色圆柱形的“蓝翔”号在虚空中逐渐变大,一声沉 ...
- PHP: 打印post数据,返回长度不为1但内容为空的问题
问题: 首先,html长这样: 我们可以看到textarea标签的name值为cten,那么我们进行查看Post是否能够正常获取到数据: 后台进行获取: 结果:我们可以正常获取到数据 接下来,我们进行 ...
- 第二章 LCD液晶显示屏&声控装置&播放音乐&遥控器
这节我将带大家了解亮宁机器人编程的基础部分. LCD液晶显示屏 LCD液晶显示屏是在实现某种功能和调试中不可缺少的部分,接下来我带大家学习,如何使用LCD液晶显示屏. 首先我们把LCD液晶显示屏插入主 ...
- LA 3938 动态最大连续和
题目链接:https://vjudge.net/contest/146667#problem/C 题意:动态的求一个区间的最大连续和. 分析: 看上去可以RMQ去做,但是,当分成两个部分,原来的部分的 ...
- Poj(2421),Prim最小生成树
题目链接:http://poj.org/problem?id=2421 最小生成树的变形,有的村庄已经连接了,就直接把他们的权值赋为0,一样的做最小生成树,Prim算法. #include <s ...
- android build.prop详解
# begin build properties开始设置系统性能 # autogenerated by buildinfo.sh{通过设置形成系统信息} ro.build.id=MIUI(版本ID) ...
- jquery 表单事件
.blur() 当元素失去焦点的时候触发事件. .blur(handler(eventObject)) handler(eventObject) 每当事件触发时候执行的函数. .blur([event ...
- 深入理解 SVG 系列(一) —— SVG 基础
来源:https://segmentfault.com/a/1190000015652209 本系列文章分为三个部分: 第一部分是 SVG 基础. 主要讲 SVG 的一些基础知识,包括 SVG 基本元 ...
- [MYSQL笔记0]MYSQL的安装
mysql是一种关系型数据库管理系统.以mysql5.7版本为例,安装过程如下: 首先百度出mysql的官网,进入:(以下是自己安装失败的过程,直接下拉最后看大佬的安装过程吧,就是那个红红的网址) 找 ...