您的位置:首页 > 其它

CodeForces 629E(#343)|Famil Door and Roads|树形dp|概率

2016-02-25 23:36 465 查看

中文翻译

描述

Famil Door的城市地图看起来像一棵树(无向无环图)所以其他人叫他Treeland。城市里有n个由n-1条双联通边连接起来的节点。

Famil Door有m个朋友生活在这城市。第i个朋友住在ui而且在vi工作。在城市里的每个人都不开心因为在他们家和工作地点间只有一条简单路径。

Famil Door计划构造1条新边而且他会在n(n−1)/2种可选方案中随机选择一个。

他知道,他的每个朋友会变得开心,只有在他修筑的新路使得有一条路径从他朋友家到工作地点而且回去使得不重复走一条边2次。形式地,这里将有一个简单环,都包含ui和vi。

此外,如果某个朋友变得开心,他的愉快值等价于这样的路径(显然它是唯一的)的长度。对于每个朋友Famil Door希望知道他的期望愉快值(即同时包含ui和vi的环的期望长度)如果我们只考虑环存在的情况。

输入

输入第一行包含两个整数n和m(2≤n,m≤100000),树的点数和朋友数。

接下来n−1行描述了一些双联通边,每行包含两个整数ai和bi,(1≤ai,bi≤n),表示第i条路连接的两个点。

最后m行描述FD的朋友,第i行包含两个整数ui和vi,(1≤ui,vi≤n,ui≠vi),表示第i个朋友的居住点和工作点。

输出

对于每个FD的朋友你需要输出如果他开心的期望愉快度。你的答案与标准答案误差不能超过10−6。

也就是说,假设你的答案是a而且标答是b,那么自定义校验器会认为你的答案是正确的仅当|a−b|max(1,b)≤10−6。

提示

样例2:

1. 边(1,2)和(2,3)可以,所以期望长度是2+32=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,i−1,i−1

第二个dfs计算qu:

有两种情况:

1. u是其父亲唯一的孩子,令newcnt=1n−cntu−1有quu=qup×(n−cntp−1)+n−cntpnewcnt

2. u不是其父亲唯一的孩子,令newcnt=1cntp−cntu−1cntd=1newcntnou=qdp×cntp−qdu×cntu−cntu−1newcntfrac=1n−cntu−1有quu=cntd×nou+qup×(n−cntp−1)+n−cntu−1frac

现在我们需要处理询问,对于每个询问u,v,我们有情况:一个是另一个的祖先或不是。第一种情况,定义w表示u(不妨设u更浅)之前的节点。v到u的路径,答案是:

cntv×(n−cntw−1)×qdv+(n−cntw−1)×(cntv+1)×quw(cntv+1)×(n−cntw−1)+hv−hu

第二种情况,定义w=LCA(u,v),答案是

cntu×(cntv+1)×qdu+(cntu+1)×quv(cntv+1)×(cntu−1)+hv−hu−2×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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: