Codeforces Round #411 (Div. 1) D. Expected diameter of a tree(树的直径)

来源:互联网 发布:淘宝客服这工作怎么样 编辑:程序博客网 时间:2024/05/23 12:13

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 21 33 12 3
output
-12.0000000000
input
5 2 32 44 34 24 12 5
output
-12.66666666672.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个询问,每次询问两棵树U V,问若U V间任意连一条边形成的新树的直径的期望是多少.


分析:我们通过几次DFS可以很容易的求出从一棵树上任意一点出发能达到的最长距离,然后如果我们连接两棵树上的任意两点,所形成的新树的期望长度就是原来两树直径的最大值和新形成的这条路径的值中的最大值,这样我们提前预处理出每棵树上所有点所能达到的最大距离和每个最大距离的其个数还有各种前缀和,然后对于一个询问U V,我们枚举U中的最大深度然后在V中二分查找MAX(D(U),D(V))的分界点,利用之前的前缀和就能很快求出对答案的贡献,然后因为要求期望所以最后要用直径长之和除以size(u)*size(v).


#include <bits/stdc++.h>#define N 100005using namespace std;typedef long long ll;int n,m,q,Max,cho,cnt,u,v,color[N],val[N],d[N];vector<int> S[N],G[N];vector<ll> pre[N],lab[N],num[N];map<int,int> t;bool vis[N];void dfs1(int u,int col,int D){if(D > Max){Max = D;cho = u;}vis[u] = 1;color[u] = col;S[col].push_back(u);for(int v : G[u]) if(!vis[v]) dfs1(v,col,D+1);}void dfs2(int u,int fa,int D){val[u] = max(val[u],D);if(D > Max){Max = D;cho = u;}for(int v : G[u])     if(v != fa) dfs2(v,u,D+1);}int main(){scanf("%d%d%d",&n,&m,&q);for(int i = 1;i <= m;i++){scanf("%d%d",&u,&v);G[u].push_back(v);G[v].push_back(u);}for(int i = 1;i <= n;i++)     if(!vis[i]) {t.clear(); Max = 0,cho = i; dfs1(i,++cnt,0); int u = cho; Max = 0; dfs2(u,0,0); int v = cho; d[cnt] = Max;dfs2(v,0,0);for(int p : S[cnt]) t[val[p]]++;for(map<int,int> :: iterator it = t.begin();it != t.end();it++){lab[cnt].push_back(it->first);num[cnt].push_back(it->second);if(num[cnt].size() > 1) num[cnt][num[cnt].size()-1] += num[cnt][num[cnt].size()-2];pre[cnt].push_back(1ll*it->first*it->second);            if(pre[cnt].size() > 1) pre[cnt][pre[cnt].size()-1] += pre[cnt][pre[cnt].size()-2];} }for(int i = 1;i <= q;i++){scanf("%d%d",&u,&v);ll ans = 0;u = color[u],v = color[v];if(u == v){cout<<-1<<endl;continue;}if(S[u].size() > S[v].size()) swap(u,v);Max = max(d[u],d[v]);for(int j = 0;j < lab[u].size();j++){int temp = Max - lab[u][j],M = num[v].size(),weight = num[u][j] - (j ? num[u][j-1] : 0);int mid = lower_bound(lab[v].begin(),lab[v].end(),temp) - lab[v].begin();if(mid) ans = ans + 1ll*weight*num[v][mid-1]*Max;if(mid < M) ans = ans + 1ll*weight*((num[v][M-1] - (mid ? num[v][mid-1] : 0))*(1ll+lab[u][j]) + pre[v][M-1] - (mid ? pre[v][mid-1] : 0));}printf("%.8f\n",ans*1.0/(1ll*S[u].size()*S[v].size()));}}


0 0
原创粉丝点击