QOJ.ac

QOJ

حد الوقت: 2.5 s حد الذاكرة: 512 MB مجموع النقاط: 100

#3628. 极限远征

الإحصائيات

年轻的计算机科学家 Kile 需要减肥,因此他决定前往 Sljeme 山。通过研究登山地图,Kile 注意到 Sljeme 山上的小径构成了一种树状结构。更准确地说,他将小径视为树的边,将小径的交汇点视为节点。

他得出结论,这棵树由 $n$ 个节点组成,并用 $1$ 到 $n$ 的自然数对它们进行了编号。随后,他计划了 $q$ 次远足,其中第 $i$ 次远足从节点 $a_i$ 开始,在节点 $b_i$ 结束。他还相当乐观地估计,经过任意两个相邻节点之间所需的时间恰好为一分钟。

然而,Kile 的方向感并不好。因此,当他到达某个节点时,他会从该节点连接的所有小径中随机且均匀地选择下一条小径。为了让 Kile 能够规划他后续的活动,他想知道对于 $q$ 次远足中的每一次,他在 Sljeme 山上攀爬的期望时间是多少。也就是说,如果他按照上述方式移动,从节点 $a_i$ 到节点 $b_i$ 的期望时间(以分钟为单位)是多少。请帮助他!

注意:可以证明所求的期望时间可以写成最简分数 $\frac{P}{R}$ 的形式。为了避免精度问题,需要输出 $P \cdot R^{-1} \pmod{10^9 + 7}$。

输入格式

第一行包含两个自然数 $n$ ($2 \le n \le 300\,000$) 和 $q$ ($1 \le q \le 300\,000$)。

接下来的 $n - 1$ 行包含两个数 $u_i$ 和 $v_i$ ($1 \le u_i, v_i \le n$),表示编号为 $u_i$ 和 $v_i$ 的节点之间直接由一条边相连。这些边构成了一棵包含 $n$ 个节点的树(即无环的简单连通图)。

接下来的 $q$ 行中,第 $i$ 行包含两个互不相同的数 $a_i$ 和 $b_i$ ($1 \le a_i, b_i \le n$),分别表示第 $i$ 次远足的起点和终点。

输出格式

对于每一行 $i$,输出第 $i$ 次远足的期望时间,如题目所述。

样例

输入 1

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

输出 1

11
10
7

输入 2

3 1
1 2
2 3
2 3

输出 2

3

说明

对于样例 2,有 50% 的概率步行在一步内结束,有 50% 的概率 Kile 会花费 2 步回到节点 2 并重新尝试。因此期望时间为 $\frac{1}{2} \cdot 1 + \frac{1}{2} \cdot (2 + \frac{1}{2} \cdot 1 + \frac{1}{2} \cdot (2 + \frac{1}{2} \cdot 1 + \frac{1}{2} (\dots))) = 3$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.