QOJ.ac

QOJ

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

#10556. 无限电池制 (Unlimited Battery Works)

الإحصائيات

有一天,Little Apple 在一棵有根树上发明了一种棋类游戏,他想和你一起玩。起初,树的每个顶点上都放置着唯一的一枚黑色棋子。在每一轮中,你可以在一个顶点上施加魔法,这会将该顶点上的棋子变为白色,无论它之前是黑色还是白色。同时,某些顶点上的棋子也会变为白色。如果你在顶点 $i$ 上施加魔法,那么当且仅当顶点 $j$ 在顶点 $i$ 的子树中,且 $i$ 和 $j$ 之间最短路径的长度(以边数为单位)不超过 $A_i$ 时,顶点 $j$ 上的棋子也会变为白色。

Little Apple 想知道将整棵树变为所有棋子均为白色的状态需要多少步。然而,作为一名优秀的程序员,你认为这太简单了。你想计算出如果你随机选择一个顶点施加魔法(假设每个顶点被选中的概率相同),将树上所有棋子变为白色所需的期望步数。

请注意: 1. 如果树上所有的棋子都已经变为白色,你将不再施加魔法。 2. 在每一轮中,可以选择树上的任意顶点。

输入格式

第一行输入包含测试用例的数量 $T$。接下来是 $T$ 个测试用例。 对于每个测试用例,第一行包含一个整数 $n$ ($1 \le n \le 50$),表示树的顶点数。第二行包含 $n$ 个整数,表示 $A_1, A_2, \dots, A_n$(如上所述,$1 \le A_i < 50$,对于 $1 \le i \le n$)。最后一行包含 $n-1$ 个整数,第 $i$ 个整数表示顶点 $i+1$ 的父节点。

保证给定的图是一棵树,且顶点 1 始终是树的根。

输出格式

对于每个测试用例,输出一行 “Case #x: y”,其中 x 是测试用例编号(从 1 开始),y 是将所有棋子变为白色所需的期望步数。

如果你的答案与正确答案的绝对误差在 $10^{-6}$ 以内,则被视为正确。

样例

输入 1

3
6
1 0 0 0 0 0
1 1 1 1 1
6
1 0 1 0 1 0
1 2 3 4 5
50
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 4 3 1 2 7 6 9 6 8 10 9 13 16 15
13 18 14 15 19 22 18 24 26 27 25 27
28 25 28 30 34 34 33 34 34 33 36 36
36 37 42 42 44 43 46 48

输出 1

Case #1: 6.000000000000
Case #2: 11.000000000000
Case #3: 224.960266916471

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.