CodeForces 629E Eamil Door and Roads 树形dp 概率

Ubuntu下的搜狗好痛苦。。

中文翻译

描述

Famil Door的城市地图看起来像一棵树(无向无环图)所以其他人叫他Treeland。城市里有n个由n-1条双联通边连接起来的节点。
Famil Door有m个朋友生活在这城市。第i个朋友住在 ui 而且在 vi 工作。在城市里的每个人都不开心因为在他们家和工作地点间只有一条简单路径。
Famil Door计划构造1条新边而且他会在 n(n1)/2 种可选方案中随机选择一个。
他知道,他的每个朋友会变得开心,只有在他修筑的新路使得有一条路径从他朋友家到工作地点而且回去使得不重复走一条边2次。形式地,这里将有一个简单环,都包含 ui vi
此外,如果某个朋友变得开心,他的愉快值等价于这样的路径(显然它是唯一的)的长度。对于每个朋友Famil Door希望知道他的期望愉快值(即同时包含 ui vi 的环的期望长度)如果我们只考虑环存在的情况。

输入

输入第一行包含两个整数n和m( 2n,m100000 ),树的点数和朋友数。
接下来 n1 行描述了一些双联通边,每行包含两个整数 ai bi , (1ai,bin) ,表示第i条路连接的两个点。
最后m行描述FD的朋友,第i行包含两个整数 ui vi (1ui,vin,uivi) ,表示第i个朋友的居住点和工作点。

输出

对于每个FD的朋友你需要输出如果他开心的期望愉快度。你的答案与标准答案误差不能超过 106
也就是说,假设你的答案是 a 而且标答是 b ,那么自定义校验器会认为你的答案是正确的仅当 |ab|max(1,b)106

提示

样例2:
1. 边(1,2)和(2,3)可以,所以期望长度是\frac{2+3}{2}=2.5$
2. 边(1,3)和(2,3)使第二个朋友高兴,与第一个朋友一行答案是2.5
3. 只有一种方案使第三个朋友高兴,即边(2,3),所以答案是3。

题解

首先,我们假设树有根节点。对于这道题,开始我们需要对一些节点u计算 qdu,cntu,paru,i,hu qdu 等于从节点 u 开始,在 u 子树中结束的长度至少为1的路径的长度的期望值。。。。 quu 等于从u开始,不在 u 子树中结束而且长度至少为1的路径的长度的期望值。计算这两个东西可以各用一个dfs解决。 cntu 是u的子树中除了 u 的节点数。 paru,i 表示u第 2i 个祖先。 hu u 的高度。
第一个dfs同时计算 qd,cnt,par

cntu=cntv+1

qdu=cntv×qdvcntu+1

paru,i=parparu,i1,i1

第二个dfs计算 qu :
有两种情况:
1. u是其父亲唯一的孩子,令
newcnt=1ncntu1
quu=qup×(ncntp1)+ncntpnewcnt
2. u不是其父亲唯一的孩子,令
newcnt=1cntpcntu1
cntd=1newcnt
nou=qdp×cntpqdu×cntucntu1newcnt
frac=1ncntu1
quu=cntd×nou+qup×(ncntp1)+ncntu1frac
现在我们需要处理询问,对于每个询问 u,v ,我们有情况:一个是另一个的祖先或不是。第一种情况,定义 w 表示 u (不妨设 u 更浅)之前的节点。 v u 的路径,答案是:
cntv×(ncntw1)×qdv+(ncntw1)×(cntv+1)×quw(cntv+1)×(ncntw1)+hvhu

第二种情况,定义 w=LCA(u,v) ,答案是
cntu×(cntv+1)×qdu+(cntu+1)×quv(cntv+1)×(cntu1)+hvhu2×hw+1

复杂度是 O(n+mlogn)

翻译略渣,欧洲人不要D。

http://beepaste.ir/view/7ae24ede

E. Famil

Door and Roads
time limit per test5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Famil Door’s City map looks like a tree (undirected connected acyclic graph) so other people call it Treeland. There are n intersections in the city connected by n - 1 bidirectional roads.

There are m friends of Famil Door living in the city. The i-th friend lives at the intersection ui and works at the intersection vi. Everyone in the city is unhappy because there is exactly one simple path between their home and work.

Famil Door plans to construct exactly one new road and he will randomly choose one among n·(n - 1) / 2 possibilities. Note, that he may even build a new road between two cities that are already connected by one.

He knows, that each of his friends will become happy, if after Famil Door constructs a new road there is a path from this friend home to work and back that doesn’t visit the same road twice. Formally, there is a simple cycle containing both ui and vi.

Moreover, if the friend becomes happy, his pleasure is equal to the length of such path (it’s easy to see that it’s unique). For each of his friends Famil Door wants to know his expected pleasure, that is the expected length of the cycle containing both ui and vi if we consider only cases when such a cycle exists.

Input

The first line of the input contains integers n and m (2 ≤ n,  m ≤ 100 000) — the number of the intersections in the Treeland and the number of Famil Door’s friends.

Then follow n - 1 lines describing bidirectional roads. Each of them contains two integers ai and bi (1 ≤ ai, bi ≤ n) — the indices of intersections connected by the i-th road.

Last m lines of the input describe Famil Door’s friends. The i-th of these lines contain two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — indices of intersections where the i-th friend lives and works.

Output

For each friend you should print the expected value of pleasure if he will be happy. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let’s assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Examples

input

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

output

4.00000000
3.00000000
3.00000000

input

3 3
1 2
1 3
1 2
1 3
2 3

output

2.50000000
2.50000000
3.00000000

Note

Consider the second sample.

Both roads (1, 2) and (2, 3) work, so the expected length if
Roads (1, 3) and (2, 3) make the second friend happy. Same as for friend 1 the answer is 2.5
The only way to make the third friend happy is to add road (2, 3), so the answer is 3

你可能感兴趣的:(动态规划,概率,codeforces,LCA,树形)