题目

给一片森林,\(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的更多相关文章

  1. CF804D Expected diameter of a tree 树的直径 根号分治

    LINK:Expected diameter of a tree 1e5 带根号log 竟然能跑过! 容易想到每次连接两个联通快 快速求出直径 其实是 \(max(D1,D2,f_x+f_y+1)\) ...

  2. Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)

    题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...

  3. Codeforces 840D Expected diameter of a tree 分块思想

    Expected diameter of a tree 我们先两次dfs计算出每个点能到达最远点的距离. 暴力计算两棵树x, y连边直径的期望很好求, 我们假设SZ(x) < SZ(y) 我们枚 ...

  4. 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 ...

  5. Codeforces Round #411 (Div. 1) D. Expected diameter of a tree

    题目大意:给出一个森林,每次询问给出u,v,问从u所在连通块中随机选出一个点与v所在连通块中随机选出一个点相连,连出的树的直径期望(不是树输出-1).(n,q<=10^5) 解法:预处理出各连通 ...

  6. codeforces804D Expected diameter of a tree

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. Codeforces 804D Expected diameter of a tree(树形DP+期望)

    [题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...

  8. CodeForces 805F Expected diameter of a tree 期望

    题意: 给出一个森林,有若干询问\(u, v\): 从\(u, v\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望. 分析: 回顾一下和树的直径有关的东西: 求树的直径 从树的任意 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. Ansible Ad-hoc,命令执行模块

    目录 Ad-hoc Ad-hoc简介 Ad-hoc命令说明 Ad-hoc示例 命令执行模块 1. command模块 2. shell模块 3. raw模块 4. script模块 Ad-hoc Ad ...

  2. python中partial用法

    应用 典型的,函数在执行时,要带上所有必要的参数进行调用.然后,有时参数可以在函数被调用之前提前获知.这种情况下,一个函数有一个或多个参数预先就能用上,以便函数能用更少的参数进行调用. 示例pyqt5 ...

  3. 【Java复健指南12】OOP高级03-抽象类与接口

    抽象类 引出 问题: ​ 父类方法有时候具有不确定性 小结: 当父类的某些方法,需要声明,但是又不确定如何实现 时,可以将其声明为抽象方法,那么这个类就是抽象类 例子: public class Ab ...

  4. DataGear 制作支持全国、省、市三级数据钻取效果的地图数据可视化看板

    通过DataGear的参数化数据集.图表联动和看板API功能,可以很方便地制作支持数据钻取效果的数据可视化看板. 首先,以上级地区名为参数,新建一个参数化SQL数据集: SELECT COL_NAME ...

  5. java数组案例

    数组:         数组就是用来存储一批同类型数据的内存区域(容器) 数组中的最大值实现方法:   数据拿到程序中去,用数组装起来. 定义一个变量,用于记录最大值.这个变量建议默认存储第一个元素作 ...

  6. Java 四种不同的权限修饰

    private 私有属性 只在同一个包下 同一个类中可以调用 同一个包下,不同的类中,可以调用 缺省,保护(protected),公共(public)可以调用, 不同的包下的类中,继承关系,可以调用 ...

  7. java线程示例

    需要开启线程 的方法继承线程类,并在run  中写逻辑 public class Ant extends Thread{ Cake cake; public Ant(String name,Cake ...

  8. Java实现书城项目(增删)

    书城项目 登录 dao 接口:UserDao Users login(String username,String password); 实现:UserDaoImpl QueryRunner quer ...

  9. Zabbix MQQT协议监控 loT设备

    一. 项目背景 监控异地局域网主机(主机内有物联5G卡 可以单方面向特定的云服务器传输信息)这里采用 zabbix 5xx系列 agent2 -6.2 版本 主动模式,即客户端向服务端注册.   二. ...

  10. 将Abp移植进.NET MAUI项目(三):构建UI层

    ​ 很开心,终于到了创建页面的时候了! 我们需要两个页面 MainPage 主页面 MusicItemPage 条目编辑页面 编写主页面 新建一个MainPageViewModel.cs,作为Main ...