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 ...
随机推荐
- Python一键安装全部依赖包
requirements.txt用来记录项目所有的依赖包和版本号,只需要一个简单的pip命令就能完成. pip freeze >requirements.txt 然后就可以用 pip insta ...
- 【weka】分类,cross-validation,数据
一.分类classifier 如何利用weka里的类对数据集进行分类,要对数据集进行分类,第一步要指定数据集中哪一列做为类别,如果这一步忘记了(事实上经常会忘记)会出现“Class index is ...
- python基础24 -----python中的各种锁
一.全局解释器锁(GIL) 1.什么是全局解释器锁 在同一个进程中只要有一个线程获取了全局解释器(cpu)的使用权限,那么其他的线程就必须等待该线程的全局解释器(cpu)使 用权消失后才能使用全局解释 ...
- selenium webdriver 截屏操作
有时候我们需要进行截屏操作,特别是遇到一些比较重要的页面信息(出现错误)或者出现不同需要进行对比时, 我们就需要对正在处理的页面进行截屏! 未经作者允许,禁止转载! package test_wait ...
- SQL Server查询中特殊字符的处理方法
SQL Server查询中,经常会遇到一些特殊字符,比如单引号“'”等,这些字符的处理方法,是SQL Server用户都应该需要知道的. 我们都知道SQL Server查询过程中,单引号“'”是特殊字 ...
- HashMap(JDK1.9)详解
一.HashMap的概念. 1.HashMap类的继承实现关系如下:因此HashMap的功能有:可序列化.可克隆等功能. 2.HashMap的数据结构:数组+链表+红黑树. 3.键值对的存储方案:第一 ...
- STA分析(一) setup and hold
timing check可以分为Dynamic Timing Analysis(Post_sim)和Static Timing Analysis STA:可以分析的很全面:仿真速度也很快:可以分析控制 ...
- bootstrap3浏览器支持情况
Internet Explorer 8 和 9 是被支持的,但是还是有很多CSS3属性和HTML5元素 -- 例如,圆角矩形和投影 -- 是肯定不被支持的.另外,Internet Explorer 8 ...
- Object-C-block
块是对c语言的一种扩展语法 块看起来像函数,不同的是,快可以直接写在函数内部 块能够作为参数传递给函数或者方法 void sayHello(){NSLog(@"hello!");} ...
- 生产者消费者模型——wait/notify/notifyAll使用
告警系统架构如下 1. 数据处理系统处理完原始数据并入库后,发送消息到kafka系统: 2. 告警生产者从kafka系统查询消息存入告警消息队列: 3. 告警消费者从告警消息队列查询消息进行处理. 这 ...