CF804D.Expected diameter of a tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn vertices and mm edges. There are qq queries we should answer. In each query two vertices vv and uu are given. Let VV be the set of vertices in the connected component of the graph that contains vv , and UU be the set of vertices in the connected component of the graph that contains uu . Let's add an edge between some vertex and some vertex in and compute the value dd of the resulting component. If the resulting component is a tree, the value dd is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of dd , if we choose vertices aa and bb 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.

输入格式

The first line contains three integers nn , mm and qq ( 1<=n,m,q<=1051<=n,m,q<=10^{5} ) — the number of vertices, the number of edges in the graph and the number of queries.

Each of the next mm lines contains two integers uiu_{i} and viv_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n ), that means there is an edge between vertices uiu_{i} and viv_{i} .

It is guaranteed that the given graph is a forest.

Each of the next qq lines contains two integers uiu_{i} and viv_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n ) — the vertices given in the ii -th query.

输出格式

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

Your answer will be considered correct if its absolute or relative error does not exceed 10610^{-6} . Let's assume that your answer is aa , and the jury's answer is bb . The checker program will consider your answer correct, if .

输入输出样例

  • 输入#1

    3 1 2
    1 3
    3 1
    2 3
    

    输出#1

    -1
    2.0000000000
    
  • 输入#2

    5 2 3
    2 4
    4 3
    4 2
    4 1
    2 5
    

    输出#2

    -1
    2.6666666667
    2.6666666667
    

说明/提示

In the first example the vertices 11 and 33 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 121-2 , the other one is 232-3 . In both ways the resulting diameter is 22 , so the answer is 22 .

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 121-2 or 131-3 the diameter is 33 , and for added edge 141-4 the diameter is 22 . Thus, the answer is .

首页