codeforces804D Expected diameter of a tree
本文版权归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的更多相关文章
- 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 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...
- 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) 解法:预处理出各连通 ...
- 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\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望. 分析: 回顾一下和树的直径有关的东西: 求树的直径 从树的任意 ...
- 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 ...
随机推荐
- TCP/UDP client/server library for Java, 最好的java语言tcp udp 服务器客户端实现库
这个库andrdoi也可以用,而且是基于类的使用方式: 它支持类似聊天室的功能,即一个人说话,所有客户端都能收到,当然也支持点点通信.它还支持 RMI 的方式调用远程过程. https://githu ...
- js自执行函数的常见写法
js自执行函数的常见写法 2016-12-20 20:02:26 1.关于自执行函数 1.1 写自执行函数的好处:独立的作用域,不会污染全局环境 (function() { })(); 1.2 理解重 ...
- django登录功能(简单在POST请求)
第一 先在templates中创立index.html !DOCTYPE html> <head> <meta charset="UTF-8"> & ...
- Object-C-block
块是对c语言的一种扩展语法 块看起来像函数,不同的是,快可以直接写在函数内部 块能够作为参数传递给函数或者方法 void sayHello(){NSLog(@"hello!");} ...
- jquery日期时间控件
代码下载地址: jquery日期时间控件下载地址 . 工作中用到, 这里分享一下, 避免重复摸索劳动. 一. HTML 文件 <!DOCTYPE HTML PUBLIC "- ...
- Jmeter响应内容显示乱码问题的解决办法
Jmeter在访问接口的时候,响应内容如果有中文可能会显示乱码,原因应该是响应页面没有做编码处理,jmeter默认按照ISO-8859-1编码格式进行解析. 下面把解决步骤列一下: 现象:jmeter ...
- Filter过滤器与Session会话跟踪技术
Filter过滤器 适用场景 1.为web应用程序的新功能建立模型(可被添加到web应用程序中或者从web应用程序中删除而不需要重写基层应用程序代码)2.用户授权Filter:负责检查用户请求,根据请 ...
- js dom 操作技巧
1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...
- JS重要的内置对象
Array对象: 属性: .length 获得数组的长度: 方法: .concat() 连接内容或者数组,组成新的数组: .join(n) 用n连接数组的每一项组成字符串,可以是空字符串: ...
- 5+App使用定位
1.定位方法 5+App定位方法:5+ API中的Geolocation模块 Geolocation目前支持h5内置的定位,百度,高德.h5内置定位支持wgs84坐标系:百度支持gcj ...