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 ...
随机推荐
- 为什么要用Markov chain Monte Carlo (MCMC)
马尔科夫链的蒙特卡洛采样的核心思想是构造一个Markov chain,使得从任意一个状态采样开始,按该Markov chain转移,经过一段时间的采样,逼近平稳分布stationary distrib ...
- Java-二分查找算法
package com.lym.binarySearch; import java.util.Arrays; /** * 二分查找 * * @author Administrator * */ pub ...
- 【环境变量】Linux 下三种方式设置环境变量
1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似的错误. 2.那么什么是环境变 ...
- uva12206 后缀数组
这题说的是给了一串字符 我们要将这个字符 中找出至少出现m次的最长字符串 一个字符课多次使用 利用后缀数组计算最长的lcp 这里有一个点 记得将后缀数组中加入一个空串 如果遇到全部相同的字符时 没办法 ...
- vue的双向数据绑定原理
原理. vue是采用数据劫持结合发布者-订阅者模式的方式, 通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时发布消息给订阅者,触发相应的监听回 ...
- Hive 数据类型转换
在Hive的日常使用中经常会遇到需要对字段进行数据类型转换的情况.Hive中的数据类型转换包括隐式转换(implicit conversions)和显式转换(explicitly conversion ...
- 打造高可靠与高性能的React同构解决方案
前言 随着React的兴起, 结合Node直出的性能优势和React的组件化,React同构已然成为趋势之一.享受技术福利的同时,直面技术挑战,在复杂场景下,挑战10倍以上极致的性能优化. 什么是同构 ...
- MySQL数据库读写分离、读负载均衡方案选择
MySQL数据库读写分离.读负载均衡方案选择 一.MySQL Cluster外键所关联的记录在别的分片节点中性能很差对需要进行分片的表需要修改引擎Innodb为NDB因此MySQL Cluster不适 ...
- Nginx启动SSL功能
Nginx启动SSL功能,并进行功能优化,你看这个就足够了 一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 nginx: [emerg] the &q ...
- Python之路----迭代器与生成器
一.迭代器 L=[1,,2,3,4,5,] 取值:索引.循环for 循环for的取值:list列表 dic字典 str字符串 tuple元组 set f=open()句柄 range() enumer ...