#直径#CF804D Expected diameter of a tree
题目
给一片森林,\(q\) 个询问,每个询问两个点,
问将这两个点所在的集合连接起来组成的新集合,它的最远两点的距离的期望值是多少。
分析
首先将以每个点为根的最大深度求出来,然后对于两棵树,
只有超过两棵树直径的最大值才可能产生新的直径,
那么直接求 \(d[x]+d[y]\geq mx\) 的 \(d[x]+d[y]\),
用小的集合查询然后记忆化就可以做到 \(O(Q\sqrt{n}\log{n})\)
代码
#include <cstdio>
#include <cctype>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
const int N=100011; typedef long long lll;
map<pair<int,int>,lll>uk; vector<int>K[N];
struct node{int y,next;}e[N<<1]; vector<lll>F[N];
int col[N],f[N],g[N],dp[N],as[N],n,m,Q,et=1,upd,len[N];
int iut(){
int ans=0; char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=ans*10+c-48,c=getchar();
return ans;
}
int max(int a,int b){return a>b?a:b;}
void dfs1(int x,int fa){
col[x]=upd;
for (int i=as[x];i;i=e[i].next)
if (e[i].y!=fa){
dfs1(e[i].y,x),dp[upd]=max(dp[upd],f[x]+f[e[i].y]+1);
if (f[x]<f[e[i].y]+1) g[x]=f[x],f[x]=f[e[i].y]+1;
else if (g[x]<f[e[i].y]+1) g[x]=f[e[i].y]+1;
}
}
void dfs2(int x,int fa){
for (int i=as[x];i;i=e[i].next)
if (e[i].y!=fa){
if (f[x]==f[e[i].y]+1){
if (f[e[i].y]<g[x]+1) g[e[i].y]=f[e[i].y],f[e[i].y]=g[x]+1;
else if (g[e[i].y]<g[x]+1) g[e[i].y]=g[x]+1;
}else{
if (f[e[i].y]<f[x]+1) g[e[i].y]=f[e[i].y],f[e[i].y]=f[x]+1;
else if (g[e[i].y]<f[x]+1) g[e[i].y]=f[x]+1;
}
dfs2(e[i].y,x);
}
}
int main(){
n=iut(); m=iut(); Q=iut();
for (int i=1;i<=m;++i){
int x=iut(),y=iut();
e[++et]=(node){y,as[x]},as[x]=et;
e[++et]=(node){x,as[y]},as[y]=et;
}
for (int i=1;i<=n;++i)
if (!col[i]) ++upd,dfs1(i,0),dfs2(i,0);
for (int i=1;i<=n;++i) K[col[i]].push_back(f[i]);
for (int i=1;i<=upd;++i) sort(K[i].begin(),K[i].end());
for (int i=1;i<=upd;++i){
len[i]=K[i].size(),F[i].resize(len[i]+1);
for (int j=len[i];j;--j)
F[i][j-1]=F[i][j]+K[i][j-1];
}
for (int i=1;i<=Q;++i){
int x=col[iut()],y=col[iut()];
if (x==y) {printf("-1\n"); continue;}
if (len[x]>len[y]) swap(x,y);
if (uk.count(make_pair(x,y))) {printf("%.8lf\n",uk[make_pair(x,y)]/(1.0*len[x]*len[y])); continue;}
lll now=max(dp[x],dp[y]),ans=now*len[x]*len[y];
for (int j=0;j<len[x];++j){
int pos=lower_bound(K[y].begin(),K[y].end(),now-K[x][j])-K[y].begin();
ans+=F[y][pos]+(len[y]-pos)*(K[x][j]-now+1);
}
uk[make_pair(x,y)]=ans;
printf("%.8lf\n",ans/(1.0*len[x]*len[y]));
}
return 0;
}
#直径#CF804D Expected diameter of a tree的更多相关文章
- CF804D Expected diameter of a tree 树的直径 根号分治
LINK:Expected diameter of a tree 1e5 带根号log 竟然能跑过! 容易想到每次连接两个联通快 快速求出直径 其实是 \(max(D1,D2,f_x+f_y+1)\) ...
- Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)
题目链接 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 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/ ...
- 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\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望. 分析: 回顾一下和树的直径有关的东西: 求树的直径 从树的任意 ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- 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 ...
随机推荐
- DVWA sqli
level low 可以看到查询请求为 http://192.168.31.xxx/vulnerabilities/sqli/?id=1&Submit=Submit# ##### id 改为 ...
- 什么是数据同步利器DataX,如何使用?
转载至我的博客 https://www.infrastack.cn ,公众号:架构成长指南 今天给大家分享一个阿里开源的数据同步工具DataX,在Github拥有14.8k的star,非常受欢迎,官网 ...
- 【Azure APIM】在APIM中实现JWT验证不通过时跳转到Azure登录页面
问题描述 在APIM中配置JWT策略,验证JWT,如果认证失败,则跳转到 Azure Entra ID 的 Login 页面. 问题解答 要实现JWT验证失败后,跳转到 Azure Entra ID ...
- 【Azure 应用服务】如何为Web Jobs 安装Python包呢?
问题描述 WebJobs 怎么安装Python包? 问题解答 第一步:登录到App Service的高级管理工具(Kudu:https://<webappname>.scm.chinacl ...
- 【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误
问题描述 在Azure中创建Function App(函数应用), 用以运行Python代码(Python Version 3.7). 通过VS Code创建一个HttpTrigger的Functio ...
- Isito 入门(二):Istio 的部署
本教程已加入 Istio 系列:https://istio.whuanle.cn 目录 2,部署 Istio 安装 Helm 部署 istio-base 部署 istiod 部署 istio-ingr ...
- SpringCloud 网关组件Gateway
官网文档: https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/ 1. 概述 1.1 什么是网关 ...
- 我见过最好的.NET/C#图片工具(裁剪、缩放、与加水印)
付费才能得到的好资源,限今天"免费"领取,月薪超30k必备技能! 资源1:高薪热门[WPF上位机+工业互联网]从零手写实战回复wpf免费领取 资源2:C#+Halcon机器视觉零基 ...
- 继续总结Python中那些简单好用的用法
上一篇文章Python中那些简单又好用的特性和用法发出后,群里的小伙伴又给补充了几个好用的用法,结合生产实用经验汇总整理如下,各位看官如有需要请自取 反射,反射是一种机制,可以在运行时获取.检查和修改 ...
- mybatis批量插入的四种方式
一.循环插入 public void insert(List<User> userList) { userList.forEach(user -> userDao.insert(us ...