QOJ.ac

QOJ

时间限制: 12 s 内存限制: 1024 MB 总分: 100

#4433. 小猫与扫地机器人

统计

Bituś 的小猫 Kapitan 非常喜欢睡觉!遗憾的是,自从它的主人决定购买 Roomba(一种扫地机器人)后,Kapitan 的平均睡眠质量显著下降。看起来这只小猫很怕 Roomba,因为它确实非常胆小。

Bituś 的房子由 $n$ 个房间和 $n-1$ 条双向走廊组成,且任意两个房间之间都可以互相到达。Bituś 注意到,每当 Roomba 进入一个有 Kapitan 在内的房间时,小猫就会立刻惊醒并逃往其中一个相邻的房间,随后继续做着抓老鼠的梦。受惊的 Kapitan 会盲目地逃跑,因此如果当前房间有多个直接相连的房间,Kapitan 会等概率地选择其中任意一个(特别地,它也可能逃回 Roomba 刚刚离开的那个房间)。

在某次漫长的夜班中,Bituś 打开了 Roomba 的应用程序,观察到今天的清洁过程中,它依次访问了房间 $a_1, \dots, a_m$。在这个序列中,同一个房间可能会出现多次,但任意两个相邻的房间必须是直接相连的。Bituś 还记得,最初小猫是在房间 $c$ 睡觉的。此外,必须满足 $a_1 \neq c$,因为警觉的 Kapitan 绝不会和 Roomba 在同一个房间睡觉!

现在 Bituś 想知道,在整个清洁过程中,Roomba 惊醒 Kapitan 的次数的期望值是多少。请帮助 Bituś 找到答案,以便他能安心回去工作。

输入格式

输入的第一行包含测试用例的数量 $z$ ($1 \le z \le 6\,000$)。接下来是各测试用例的描述。

每个测试用例的第一行包含两个整数 $n, c$ ($2 \le n \le 1\,000\,000, 1 \le c \le n$),分别表示 Bituś 家中房间的数量和 Kapitan 最初睡觉的房间编号。

接下来的 $n-1$ 行描述走廊。每行包含两个整数 $u_i, v_i$ ($1 \le u_i, v_i \le n, u_i \neq v_i$),表示房间 $u_i$ 和 $v_i$ 之间相连。你可以假设任意两个房间之间都是连通的。

下一行包含 Roomba 在清洁过程中访问的房间数量 $m$ ($1 \le m \le 5\,000\,000$)。

最后一行包含 $m$ 个整数 $a_i$ ($1 \le a_i \le n$),表示 Roomba 访问的房间序列(按访问顺序)。任意两个相邻的房间之间都有走廊相连;此外,假设 $a_1 \neq c$。

所有测试用例中 $n + m$ 的总和不超过 $12\,000\,000$。

输出格式

对于每个测试用例,输出一个实数 $e$,表示 Roomba 进入有 Kapitan 在内的房间次数的期望值。如果你的答案与正确值的绝对误差或相对误差不超过 $10^{-5}$,则视为正确。即,如果你的答案是 $a$,正确值是 $b$,则当 $\frac{|a-b|}{\max(1, b)} \le 10^{-5}$ 时,你的答案将被接受。

样例

样例输入 1

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

样例输出 1

1.666666666666667

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.