本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:codeforces804D

正解:二分$+DP$

解题报告:

  预处理出每个点所在的树内的最远点距离dis,考虑对于树$A$的点$x$,与树$B$的$y$产生的贡献就是$dis[x]+dis[y]+1$和两棵树的直径取一个$max$。

  对于$max$,我们显然可以分开考虑,当$dis[x]+dis[y]+1<$两棵树的直径,那么贡献就是直径的$max$。

  否则我们可以直接得到贡献。

  具体做法就是先对于每棵树内部的$dis$排序,然后每次枚举较小的连通块内的每个点,二分另一个连通块中的分界点位置就好了。

  看上去像个暴力,但是仔细想想,套上记忆化之后复杂度应该很靠谱,大概是根号左右。

//It is made by ljh2000
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#define lc root<<1
#define rc root<<1|1
#define reg(i,x) for(int i=first[x];i;i=nxt[i])
using namespace std;
typedef long long LL;
const int MAXN = 200011;
const int MAXM = 400011;
int n,m,q,ecnt,first[MAXN],nxt[MAXM],to[MAXM],size[MAXN],bel[MAXN],cnt,f[MAXN][2],D[MAXN];
double ans;
map<int,double>mp[MAXN];
vector<int>w[MAXN];
vector<int>w2[MAXN];
inline void link(int x,int y){ nxt[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void dfs(int x,int fa,int id){
bel[x]=id; size[id]++;
reg(i,x) {
int v=to[i]; if(v==fa) continue;
dfs(v,x,id);
if(f[v][0]+1>f[x][0]) f[x][1]=f[x][0],f[x][0]=f[v][0]+1;
else if(f[v][0]+1>f[x][1]) f[x][1]=f[v][0]+1;
}
D[id]=max(D[id],f[x][0]+f[x][1]);
} inline void dfs2(int x,int fa,int maxl){
int tmp=max(maxl,f[x][0]);
w[ bel[x] ].push_back(tmp);
w2[ bel[x] ].push_back(0);
reg(i,x) {
int v=to[i]; if(v==fa) continue;
if(f[v][0]+1==f[x][0]) dfs2(v,x,max(maxl,f[x][1])+1);
else dfs2(v,x,max(maxl,f[x][0])+1);
}
} inline int getp(int x,int val){
int l=0,r=w[x].size()-1,mid,pos;
if(val>w[x][r]) return r;
if(val<w[x][0]) return 0;
while(l<=r) {
mid=(l+r)>>1;
if(w[x][mid]<=val) pos=mid,l=mid+1;
else r=mid-1;
}
return pos+1;
} inline void work(){
n=getint(); m=getint(); q=getint(); int x,y,r1,r2;
for(int i=1;i<=m;i++) {
x=getint(); y=getint();
link(x,y); link(y,x);
} for(int i=1;i<=n;i++)
if(!bel[i]) {
dfs(i,0,++cnt);
dfs2(i,0,0);
} for(int i=1;i<=cnt;i++) sort(w[i].begin(),w[i].end());
for(int i=1;i<=cnt;i++) {
w2[i][ w2[i].size()-1 ]=w[i][ w[i].size()-1 ];
//printf("---%d\n",w2[i][ w[i].size()-1 ]);
for(int j=w2[i].size()-2;j>=0;j--) {
w2[i][j]=w2[i][j+1]+w[i][j];
//printf("---%d\n",w2[i][j]);
}
} while(q--) {
x=getint(); y=getint();
r1=bel[x]; r2=bel[y]; if(size[r1]>size[r2]) swap(r1,r2),swap(x,y);
if(mp[r1][r2]!=0) ans=mp[r1][r2];
else {
if(r1==r2) { puts("-1"); continue; }
int lim=max(D[r1],D[r2]),pos;
ans=0;
for(int i=0,ss=w[r1].size();i<ss;i++) {
pos=getp(r2,lim-1-w[r1][i]);
ans+=(double)pos*lim; if(pos<w[r2].size()) ans+=w2[r2][pos]+(w[r1][i]+1)*(w[r2].size()-pos);
}
ans/=size[r1];
ans/=size[r2];
mp[r1][r2]=ans;
}
printf("%.8lf\n",ans);
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("804.in","r",stdin);
freopen("804.out","w",stdout);
#endif
work();
return 0;
}

  

codeforces804D Expected diameter of a tree的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. 543. Diameter of Binary Tree

    https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...

  9. 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. 使用vue如何默认选中单选框

    使用了vue以后,发现这真的是一个灵活高效的框架,能够轻松实现页面的实时刷新.那么,今天先聊聊单选框的使用.一般我们使用单选框,会这么写: //HTML <input type=" c ...

  2. WebService之Axis2(1):用POJO实现0配置的WebService

    Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物.Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebService,同时还支持S ...

  3. Windows2008 IIS配置FTP站点

    视频教程:http://vodcdn.video.taobao.com/player/ugc/tb_ugc_pieces_core_player_loader.swf?version=1.0.2015 ...

  4. django的分页器

    Django中分页器的使用 django分页器模块 #分页器 from django.core.paginator import Paginator,EmptyPage,PageNotAnIntege ...

  5. 024-linux中动态库libXXX.so

    1.动态库的概念.动态链接库与普通的程序相比而言,没有main函数,是一系列函数的实现.通过shared和fPIC编译参数生产so动态链接库文件.程序在调用库函数时,只需要连接上这个库即可. 2.动态 ...

  6. TouchSlide触屏滑动特效插件的使用

    官方连接:http://www.superslide2.com/TouchSlide/ TouchSlide 是纯javascript打造的触屏滑动特效插件,面向手机.平板电脑等移动终端, 能实现触屏 ...

  7. nginx之rewrite重写,反向代理,负载均衡

    rewrite重写(伪静态): 在地址栏输入xx.com/user-xxx.html, 实际上访问的就是xxx.com/user.php?id=xxx rewrite就这么简单 附上ecshop re ...

  8. Python2 简明教程

    Python 由 Guido Van Rossum 在90年代初创建. 它现在是最流行的语言之一 我喜爱python是因为它有极为清晰的语法,甚至可以说,它就是可以执行的伪代码. 注意: 这篇文章针对 ...

  9. [转载]ASP.NET中IsPostBack详解

    1.IsPostBack介绍Page.IsPostBack是一个标志:当前请求是否第一次打开. 调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者 ...

  10. 配置mysql主从数据库

    来源地址:https://www.cnblogs.com/alvin_xp/p/4162249.html Mysql主从配置,实现读写分离 大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡 ...