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.
The first line contains three integers n, m 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.
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
.
3 1 2 1 3 3 1 2 3
-1 2.0000000000
5 2 3 2 4 4 3 4 2 4 1 2 5
-1 2.6666666667 2.6666666667
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
.
思路:先预处理每棵树的直径z[]及里面的每个点离最远点的距离dis[i],然后枚举A树的点i,二分B树的点j,二分这个dis[i]+dis[j]+1 > max(z[A], z[B])。
# include <bits/stdc++.h>
# define pb push_back
# define mp make_pair
# define PII pair<int, int>
# define A first
# define B second
# define LL long long
using namespace std;
const int maxn = 1e5+8;
int n, dis[maxn]={0}, f[maxn]={0}, z[maxn]={0};
vector<int>v[maxn], son[maxn],str[maxn];
vector<LL>pre[maxn];
map<PII, double>M;
PII dfs1(int cur, int pre, int root)
{
f[cur] = root;
PII now = mp(0, cur);
for(auto to : v[cur])
{
if(to == pre) continue;
PII tmp = dfs1(to, cur, root);
if(tmp.A +1 > now.A) now = mp(tmp.A+1, tmp.B);
}
return now;
}
void dfs2(int cur, int pre, int d)
{
dis[cur] = max(dis[cur], d);
for(auto to : v[cur])
{
if(to == pre) continue;
dfs2(to, cur, d+1);
}
}
void init()
{
for(int i=1; i<=n; ++i)
{
if(f[i] == 0)
{
PII a = dfs1(i, 0, i);
PII b = dfs1(a.B, 0, i);
dfs2(a.B, 0, 0), dfs2(b.B, 0, 0);
z[i] = b.A;
}
}
for(int i=1; i<=n; ++i) son[f[i]].pb(i);
for(int i=1; i<=n; ++i)
{
if(son[i].size())
{
for(auto j : son[i]) str[i].pb(dis[j]);
sort(str[i].begin(), str[i].end());
pre[i].pb((LL)str[i][0]);
for(int j=1; j<str[i].size(); ++j) pre[i].pb(pre[i][j-1]+(LL)str[i][j]);
}
}
}
double cal(int a, int b)
{
if(M.count(mp(a, b))) return M[mp(a,b)];
double tmp = 0;
for(auto i : str[a])
{
int pos = upper_bound(str[b].begin(), str[b].end(), max(z[a], z[b])-i-1)-str[b].begin();
if(pos == str[b].size()) tmp += max(z[a], z[b])*1.0*son[b].size();
else
tmp += max(z[a], z[b])*1.0*pos + (i+1)*1.0*(son[b].size()-pos)+pre[b].back()-(pos==0?0:pre[b][pos-1]);
}
tmp /= (son[a].size()*1.0*son[b].size());
M[mp(a,b)] = tmp;
return tmp;
}
int main()
{
int m, q;
scanf("%d%d%d",&n,&m,&q);
while(m--)
{
int a, b;
scanf("%d%d",&a,&b);
v[a].pb(b), v[b].pb(a);
}
init();
while(q--)
{
int a, b;
scanf("%d%d",&a,&b);
if(f[a] == f[b]) {puts("-1"); continue;}
if(son[f[a]].size() > son[f[b]].size()) swap(a, b);
printf("%.10f\n",cal(f[a], f[b]));
}
return 0;
}
针对一片森林中的多个组件,通过预先处理每棵树的直径及其各节点到最远节点的距离,来高效解决关于任意两个节点所在组件连接后的期望直径计算问题。
15万+

被折叠的 条评论
为什么被折叠?



