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 ...
随机推荐
- due to a StackOverflowError. Possible root causes include a too low。。
我们可以用另外的办法来解决这个问题,我们让tomcat不扫描指定的jar包,tomcat就要轻松得多了,org.apache.tomcat.util.scan.StandardJarScanner中定 ...
- angularjs ng-if 慎用 备忘
ng-if.ng-show一般情况下可以通用,二者的最明显区别就是: ng-if判断为false时,页面dom节点不会被创建,其子节点下也不会渲染,从而也就加快了dom的加载速度:ng-show则仅是 ...
- Struts_ActionWildcard_通配符配置
使用通配符,将配置量降到最低 不过,一定要遵守“约定由于配置”的原则 struts2.xml <?xml version="1.0" encoding="UTF-8 ...
- WEB渗透测试之三大漏扫神器
通过踩点和查点,已经能确定渗透的目标网站.接下来可以选择使用漏扫工具进行初步的检测,可以极大的提高工作的效率. 功欲善其事必先利其器,下面介绍三款适用于企业级漏洞扫描的软件 1.AWVS AWVS ( ...
- 解压war包
unzip cat-alpha-3.0.0.war -d /tmp/test 说明:-d指定解压的路径和文件,文件名不存在会自动创建
- 去重算法-hash-set
Well, as Bavarious pointed out in a comment, Apple's actual CoreFoundation source is open and availa ...
- 2018.7.6 js实现点击事件---点击小图出现大图---时间定时器----注册表单验证
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 2017.9.26 request请求参数用法
4.2 访问请求参数 request对象的getParamter()方法,可以用来获取用户(客户端)提交的数据 4.2.1 访问请求参数的方法 String 自符串变量 =request.getPar ...
- 整个trick
数据输入方面:1.image pyramid 图像金字塔.目前代码里是先选取一个scale,然后在每个GPU上按照scale读图片,相应的gt也更改."scales":[440, ...
- 前端jQuery基本语法
1.概念 1.1基础知识 jQuery是一个兼容多浏览器的JavaScript库,封装了开发过程中常用的一些功能,类似Python模块 jQuery就是用JS写的,JS是基础 jQuery写起来简单, ...