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 ...
随机推荐
- 第四章 T-SQL编程
1.前言->此T-SQL编程是基于sql server开发环境->关键字:T-SQL编程:游标:视图和索引 2.T-SQL编程基础->标识符:常规标识符必须以汉字.字母.下划线_.@ ...
- 来自SaberSama的HTML总结
html 为什么要转过来呢? 因为我觉到,同样是一个初学者,应该互相学习,交流. html:是Hyper Text Markup Language的简写,即超文本标记语言. 网页的组成成分为HTML- ...
- Linux安装 NTFS 支持
my system kernel Version is 2.6.18-128.el5 在这个地址下载了 kernel-module-ntfs-2.6.18-128.el5-2.1.27-0.rr.10 ...
- 在ABAP里取得一个数据库表记录数的两种方法
方法1:使用函数EM_GET_NUMBER_OF_ENTRIES 这个函数使用起来很简单,只需要将想查询的数据库表名称维护进输入参数IT_TABLES: 上图说明这个函数支持批量操作,我查询的两张表名 ...
- 修改Windows下的Memcached下的服务端口
在命令模式下,可以使用 memcached -p 12000 去指定端口,可服务安装后,却总是只有 -d runservice 的参数. 通过修改注册表来达到这个修改端口的目的. 在 HKEY_LOC ...
- 关于使用Encoding转码的问题,以及StreamWriter的小应用
StreamWriter write = new StreamWriter("../../test2.txt"); write.WriteLine("中国123巴西red ...
- ios 创建自己的.a文件
1:首先创建个 静态工程(Cocoa Touch Static Library); 方法名字,一定要暴露在.h文件中, 2:分别在模拟器环境和真机环境下 Analyze (shift+command+ ...
- Math.random()随机生成x~y间的数字
JS如何随机产生数字呢?这就用到了Math.random()方法,它能够随机产生0~1间的数字,这个数可能为0,但会小于1. 那么,如果我想要大于等于1小于10之间的随机整数呢?需要分为以下几步: 1 ...
- Vuex基础-Getter
官方地址:https://vuex.vuejs.org/zh/guide/getters.html Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性).就像 ...
- data-ng-repeat 指令
data-ng-repeat指令对于集合中的每一项会克隆一次HTML元素.