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 ...
随机推荐
- Python用户交互与流程控制
1. 用户交互 python3通过input实现用户交互,与python2的raw_input一样,接收的值都转换成字符串格式.python2中也有一个input,而python2中的input接收的 ...
- 在IE中解决当前安全设置不允许下载该文件的方案
解决方案一: 1.0打开IE后,单击菜单栏中的“工具”菜单,在弹出的菜单中选择“Internet选项”命令: 2.0在弹出“Internet选项”的对话框中,打开“Internet选项”对话框: 3. ...
- 委托代码func和Action的基本用法
这是微软简化后的委托写法,其中,func适合带返回参数的方法调用,action适合没有返回参数的方法调用 FUNC方法代码: public string GetPeopleInfo(string na ...
- JAVA对list集合进行排序Collections.sort()
对一个集合中的对象进行排序,根据对象的某个指标的大小进行升序或降序排序.代码如下: // 进行降序排列 Collections.sort(list, new Comparator<ResultT ...
- POJ-1469 COURSES---二分图最大匹配--匈牙利算法
题目链接: https://vjudge.net/problem/POJ-1469 题目大意: 给你p门课程和n个学生,一个学生可以选0门,1门,或者多门课程,现在要求一个由p个学生组成的集合,满足下 ...
- Linux ELF格式分析
http://www.cnblogs.com/hzl6255/p/3312262.html ELF, Executable and Linking Format, 是一种用于可执行文件.目标文件.共享 ...
- 缓存验证Last-Modifie和Etag的使用
看这张图,是浏览器发出请求到请求缓存到过程,这么一个原理 那么http里面如何进行验证呢?主要有两个验证到http头 Last-Modified(上次修改时间) 主要配合If-Modified-Sin ...
- 2017.10.29 C/C++/C#程序如何打成DLL动态库
C/C++程序如何打成DLL动态库: **1.在VS中新建main.h,添加如下内容:** extern "C" _declspec(dllexport) int onLoad() ...
- OnCollisionEnter和OnTriggerEnter
之前对这两个的用法不是特别清楚,重新学习了下,再做个测试看看效果如何: 1.新建一个场景test 2.添加一个cube,点击Inspector面板会发现系统已经默认添加了Box collisder组件 ...
- Qlikview 数据加载方法罗列
以下是通常会用到的数据加载的方法,供大家参考: 1. 从文件加载: Data: Load *,RowNo() as InputKey; SQL SELECT ID,TEST,DATECREATED F ...