D. Expected diameter of a tree
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem.

We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the set of vertices in the connected component of the graph that contains v, and U be the set of vertices in the connected component of the graph that contains u. Let's add an edge between some vertex  and some vertex in  and compute the value d of the resulting component. If the resulting component is a tree, the value d is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of d, if we choose vertices a and b from the sets uniformly at random?

Can you help Pasha to solve this problem?

The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices.

Note that queries don't add edges to the initial forest.

Input

The first line contains three integers nm and q(1 ≤ n, m, q ≤ 105) — the number of vertices, the number of edges in the graph and the number of queries.

Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n), that means there is an edge between vertices ui and vi.

It is guaranteed that the given graph is a forest.

Each of the next q lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the vertices given in the i-th query.

Output

For each query print the expected value of d as described in the problem statement.

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if .

Examples
input
3 1 2
1 3
3 1
2 3
output
-1
2.0000000000
input
5 2 3
2 4
4 3
4 2
4 1
2 5
output
-1
2.6666666667
2.6666666667
Note

In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2.

In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is .

题意:

给出一个森林,q次询问,每次问把x,y两点所属的树之间任意连接一条边形成新的树的直径的期望,如果x和y在同一棵树中输出-1;

代码:

//这题算出复杂度也就解出来了。先枚举一棵树中的节点然后二分找另一棵树中的节点满足两个节点之间的距离不小于max(树1直径,
//树2直径),他们的贡献就是各自在自己树中最远能到达的端点的距离相加再+1,否则贡献就是max(树1直径,树2直径),这样看似是
//q*n*long(n),但是注意到所有的树的大小总和是n所以最坏是sqrt(n)棵树每棵树大小是sqrt(n),所以是q*sqrt(n)*long(n);
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=;
int fa[MAXN],head[MAXN],tot,n,cnt,m,q,d[MAXN],f[MAXN],deep,o,oo,root[MAXN];
int a[MAXN],aa;
double size[MAXN];
map<pair<int,int>,double>mp;
vector<ll>v[MAXN],vv[MAXN];
struct Edge { int u,v,next; }edge[MAXN*];
void init()
{
tot=cnt=;
memset(head,-,sizeof(head));
memset(fa,-,sizeof(fa));
memset(f,,sizeof(f));
memset(d,-,sizeof(d));
}
void add(int x,int y)
{
edge[tot].u=x;edge[tot].v=y;
edge[tot].next=head[x];
head[x]=tot++;
edge[tot].u=y;edge[tot].v=x;
edge[tot].next=head[y];
head[y]=tot++;
}
void dfs1(int x,int father,int p)
{
v[p].push_back(x);
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].v;
if(y==father) continue;
fa[y]=p;
dfs1(y,x,p);
}
}
void dfs2(int x,int father,int sum,bool w)
{
if(w!=) f[x]=max(f[x],sum);
if(sum>=deep){
deep=sum;
if(w==) o=x;
else if(w==) oo=x;
}
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].v;
if(y==father) continue;
dfs2(y,x,sum+,w);
}
}
int main()
{
//freopen("in.txt","r",stdin);
init();
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
for(int i=;i<=n;i++){
if(fa[i]!=-) continue;
fa[i]=++cnt;
v[cnt].clear();vv[cnt].clear();
dfs1(i,,cnt);
root[cnt]=i;
}
for(int i=;i<=cnt;i++){
deep=;
dfs2(root[i],,,);
deep=;
dfs2(o,,,);
d[i]=deep;
dfs2(oo,,,);
aa=v[i].size();
for(int j=;j<aa;j++) a[j]=f[v[i][j]];
v[i].clear();
for(int j=;j<aa;j++) v[i].push_back(a[j]);
sort(v[i].begin(),v[i].end());
a[aa]=;
for(int j=aa-;j>=;j--) a[j]=a[j+]+v[i][j]+;
for(int j=;j<=aa;j++) vv[i].push_back(a[j]);
}
while(q--){
int x,y;
scanf("%d%d",&x,&y);
if(fa[x]==fa[y]) printf("-1\n");
else{
pair<int,int>p1(fa[x],fa[y]);
if(mp[p1]>) printf("%.6f\n",mp[p1]);
else{
double ans=;
int xx=fa[x],yy=fa[y];
if(v[xx].size()<=v[yy].size()){
for(int i=;i<v[xx].size();i++){
ll tmp=lower_bound(v[yy].begin(),v[yy].end(),max(d[xx],d[yy])-v[xx][i]-)-v[yy].begin();
ans+=(vv[yy][tmp]+v[xx][i]*(v[yy].size()-tmp))+tmp*max(d[xx],d[yy]);
}
}else{
for(int i=;i<v[yy].size();i++){
ll tmp=lower_bound(v[xx].begin(),v[xx].end(),max(d[xx],d[yy])-v[yy][i]-)-v[xx].begin();
ans+=(vv[xx][tmp]+v[yy][i]*(v[xx].size()-tmp))+tmp*max(d[xx],d[yy]);
}
}
double tmp1=v[xx].size(),tmp2=v[yy].size();
ans/=(tmp1*tmp2);
printf("%.6f\n",ans);
mp[p1]=ans;
}
}
}
return ;
}

Codeforces 804D Expected diameter of a tree的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. codeforces804D Expected diameter of a tree

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

  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. Teamproject Week7 --Scrum Meeting #1 2014.10.28

    这是团队的第一次会议,具体议题如下: 1)我们明确了团队成员的职责所需: PM职责:根据项目范围.质量.时间与成本的综合因素的考虑,进行项目的总体规划与阶段计划.  控制项目组各成员的工作进度,即时了 ...

  2. Last Daily Scrum (2015/11/9)

    今晚我们终于完成了新版本的爬虫工作,可以替换掉之前部署在服务器上的那个爬虫了.由于周末大家各种原因导致了我们迭代一的截止日没有完成所有任务,所以今天晚上大家加班加点终于把这一迭代的爬虫项目完成了. 成 ...

  3. Mevan(转)

    Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0问题解决 ojdbc包pom.xml出错 置顶 2017年08月23日 10:55:25 阅读数:96 ...

  4. beta冲刺(5/7)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4 ...

  5. SQL语句中order_by_、group_by_、having的用法区别

    order by 从英文里理解就是行的排序方式,默认的为升序. order by 后面必须列出排序的字段名,可以是多个字段名. group by 从英文里理解就是分组.必须有“聚合函数”来配合才能使用 ...

  6. 转 js事件探秘

    Javascript中的事件,可以和html交互. 事件流IE&Opera:事件冒泡其他浏览器: 事件捕获 事件冒泡:事件由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播至 ...

  7. 在dell服务器上装windows server 2012详细解析

    壹: 首先确定磁盘阵列的问题,在dell服务器开机后按住 Ctrl+R 或者 F2 会展开虚拟磁盘创建菜单 详细步骤可以查看:https://jingyan.baidu.com/article/915 ...

  8. XMind2TestCase:一个高效测试用例设计的解决方案!

    一.背景 软件测试过程中,最重要.最核心就是测试用例的设计,也是测试童鞋.测试团队日常投入最多时间的工作内容之一. 然而,传统的测试用例设计过程有很多痛点: 1.使用Excel表格进行测试用例设计,虽 ...

  9. Linux里的稀疏文件

    今天发现一个有意思的现象,文件系统大小只有37GB,上面却有一个900GB的文件!查了下,这个叫“稀疏文件”,我理解类似于VMWare里的瘦硬盘模式吧,先预先划出一块空间,然后往里填数据. [root ...

  10. Linux 压缩 解压缩 命令相关

    1.命令格式:tar[必要参数][选择参数][文件] 2.命令功能:用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的 3.命令参数:必要参数有如下:-A 新增压缩文件到已存在的压缩 ...